/* Chat Interface Styles */
.chat-container {
  width: 100%;
  height: 100%;
  max-width: 100%;
  background-color: var(--query-editor-bg);
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  --screen-max-width: 48rem;
  position: relative;

  .scroll-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
    overflow: auto;
    position: relative;

    >.header {
      display: flex;
      align-items: center;
      justify-content: space-between;
      width: 100%;
      padding: 0.4rem 0;
      position: sticky;
      top: 0;
      background-color: var(--query-editor-bg);
      z-index: 1;

      .settings-btn {
        min-width: 0;
        width: 1.8rem;
        height: 1.8rem;
        border: none;
        border-radius: 9999px;
        cursor: pointer;
        margin-left: auto;
        margin-right: 0.3rem;
        box-shadow: none;
        padding: 0;

        .title-popup {
          right: 0;
        }

        &:hover {
          opacity: 1;
        }

        .material-symbols-outlined {
          font-size: 20px;
        }
      }
    }
  }

  &.empty-chat {

    .scroll-container,
    .chat-input-container-container {
      flex-grow: 1;
    }

    .scroll-container>.header {
      margin-bottom: auto;
    }

    .plugin-title {
      text-align: center;
      margin-bottom: 2rem;
    }

    .chat-messages {
      display: none;
    }
  }

  &:not(.empty-chat) {
    .plugin-title {
      display: none;
    }

    .scroll-container {
      flex-grow: 1;
    }
  }

  .scroll-down-btn {
    z-index: 1;
    position: sticky;
    bottom: 0.5rem;
    left: 50%;
    transform: translateX(-50%);
    min-width: 0;
    width: 2rem;
    height: 2rem;
    padding: 0;
    border: 1px solid var(--border-color);
    border-radius: 9999px;
    color: var(--text);
    background-color: var(--query-editor-bg);
    display: flex;
    align-items: center;
    justify-content: center;

    &:hover {
      background-color: color-mix(in srgb, var(--theme-base) 10%, var(--query-editor-bg));
    }

    .material-symbols-outlined {
      font-size: 1.6rem;
      margin-top: 0.2rem;
    }
  }
}

.chat-messages {
  flex: 1;
  width: 100%;
  max-width: var(--screen-max-width);
  background-color: var(--theme-bg-chat);
  display: flex;
  flex-direction: column;

  >.spinner {
    padding-inline: 1.8rem;
  }
}

.message {
  margin-inline: 0.6rem;
  border-radius: 16px;
  max-width: 80%;
  word-wrap: break-word;

  &.user {
    margin-block: 1rem;
  }
}

.message-content {
  display: flex;
  flex-direction: column;
  gap: 1.2rem;
  padding: 0.55rem 0.9rem;
  line-height: 1.4rem;
  white-space: pre-wrap;

  p,
  pre,
  code {
    margin: 0;
  }

  pre {
    overflow-x: auto;
    display: inline-block;
    background-color: var(--bks-text-editor-bg-color);
    color: var(--bks-text-editor-fg-color);
    padding: 0.5rem;
    width: 100%;
    line-height: 1.07rem;
    font-size: 0.765rem;

    code {

      &::selection,
      *::selection {
        background-color: var(--bks-text-editor-selected-bg-color);
      }
    }
  }

  .markdown {
    white-space: normal;
    display: flex;
    flex-direction: column;
    gap: 1.2rem;

    ol,
    ul {
      margin-block-start: 0.8em;
      margin-block-end: 0.8em;
      padding-inline-start: 2rem;
    }

    think {
      color: rgb(from var(--text-dark) r g b / 50%);
    }
  }

  .code-block {
    border: 1px solid var(--border-color);
    border-radius: 8px;
    white-space: normal;
    --header-bg-color: color-mix(in srgb, var(--theme-base) 5%, var(--query-editor-bg));

    .lang {
      padding-inline: 0.5rem;
      height: 1.8rem;
      display: flex;
      justify-content: space-between;
      font-size: 0.75rem;
      line-height: 1.8rem;
      color: var(--text);
      background-color: var(--header-bg-color);
      border-top-left-radius: 8px;
      border-top-right-radius: 8px;
    }

    .actions {
      position: sticky;
      top: 4.75rem;
      margin-right: 0.3rem;

      .group {
        position: absolute;
        bottom: 0rem;
        right: 0;
        display: flex;
        justify-content: flex-end;
        gap: 0.25rem;
      }

      .btn {
        min-width: 0px;
        padding: 0 0;
        width: 1.75rem;
        height: 1.75rem;
        box-shadow: none;
        --btn-bg-color: var(--header-bg-color);

        &:disabled {
          opacity: 1;
          color: color-mix(in srgb, var(--theme-base) 50%, var(--query-editor-bg));
        }

        .material-symbols-outlined {
          font-size: 1rem;
        }

        &.copied .copy-label,
        &.copied .copy-icon,
        &:not(.copied) .copied-label,
        &:not(.copied) .copied-icon {
          display: none;
        }
      }

      [data-action=open-in-query-editor] .title-popup {
        max-width: 10rem;

        @media (min-width: 840px) {
          max-width: unset;
        }
      }
    }
  }

  .hljs-keyword {
    color: var(--bks-text-editor-keyword-fg-color);
  }

  .hljs-operator {
    color: var(--bks-text-editor-operator-fg-color);
  }

  .hljs-comment {
    color: var(--bks-text-editor-comment-fg-color);
  }

  .hljs-string {
    color: var(--bks-text-editor-string-fg-color);
  }

  .hljs-title.class_ {
    color: var(--bks-text-editor-class-fg-color);
  }

  .hljs-title.function_ {
    color: var(--bks-text-editor-function-fg-color);
  }

  .hljs-number {
    color: var(--bks-text-editor-number-fg-color);
  }

  .hljs-attr {
    color: var(--bks-text-editor-attributeName-fg-color);
  }

  .hljs-built_in {
    color: var(--bks-text-editor-builtin-fg-color);
  }
}

.message.user {
  align-self: flex-end;

  .message-content {
    border-radius: 1rem;
    background-color: var(--theme-bg-message-user);
    color: var(--text-dark);
    border-bottom-right-radius: 4px;
  }
}

.message.assistant,
.message.system,
.message.tool {
  max-width: none;

  .message-content {
    color: var(--theme-text-message-system);
  }
}

.tool {
  display: flex;
  flex-direction: column;
  padding: 0.5rem 1rem;
  border-left: 2px solid var(--border-color);
  gap: 0.35rem;
  width: 100%;

  .accept-icon,
  .reject-icon {
    font-size: 1.2rem;
  }
}

.message.error {
  background-color: rgb(from var(--brand-danger) r g b / 20%);
  border: 1px solid rgb(from var(--brand-danger) r g b / 100%);
  max-width: none;

  .message-content {
    white-space: normal;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;

    pre {
      background-color: rgb(from var(--brand-danger) r g b / 10%);
      border-radius: 8px;
      text-wrap: wrap;
    }

    .btn {
      align-self: flex-start;
      border: 1px solid rgb(from var(--brand-danger) r g b / 30%);
      background-color: rgb(from var(--brand-danger) r g b / 5%);
      color: var(--text-dark);

      &:hover {
        background-color: rgb(from var(--brand-danger) r g b / 10%);
      }

      &:focus {
        outline: none;
        box-shadow: 0 0 0 2px rgb(from var(--brand-danger) r g b / 20%);
      }

      .material-symbols-outlined {
        font-size: 1.1rem;
        margin-right: 0.3rem;
      }
    }

    .error-hint {
      background-color: rgb(from var(--brand-warning) r g b / 15%);
      border: 1px solid rgb(from var(--brand-warning) r g b / 30%);
      border-radius: 4px;
      padding: 0.5rem 0.75rem;
      margin-bottom: 0.75rem;
      font-size: 0.75rem;
      color: var(--text-dark);
      line-height: 1.3;
    }
  }
}

.tool-call {
  padding-left: 1rem;
  border-left: 2px solid var(--border-color);

  .tool-call-name {
    display: flex;
    align-items: center;

    .accept-icon {
      color: color-mix(in srgb, var(--brand-success) 70%, black);
    }

    .reject-icon {
      color: color-mix(in srgb, var(--brand-danger) 90%, black);
    }
  }
}

.tool-call.active {
  justify-content: center;
  align-items: center;
}

.tool-permission-buttons {
  display: flex;
  gap: 0.5rem;
  margin-top: 0.2rem;

  button {
    border: none;
    background-color: transparent;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: currentColor;
    padding: 0;

    &:hover {
      .accept-icon {
        color: color-mix(in srgb, var(--brand-success) 70%, black);
      }

      .reject-icon {
        color: color-mix(in srgb, var(--brand-danger) 90%, black);
      }
    }
  }
}

.chat-input-container-container {
  width: calc(100% - 2rem);
  max-width: var(--screen-max-width);
  margin: 0rem 1rem 1rem;
}

.chat-input-container {
  background-color: rgb(from var(--theme-base) r g b / 5%);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  width: 100%;

  .base-input {
    flex: initial;
    font-size: 0.9rem;
    max-height: 30vh;
    line-height: 1.25rem;
    border-radius: 8px;

    textarea {
      border-color: transparent;
    }
  }

  .actions {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    padding: 0.5rem;
    gap: 0.5rem;

    .dropdown-trigger {
      padding: 0.3rem 0.4rem;
      border: none;
      border-radius: 0.3rem;
      background-color: transparent;
      color: var(--text-dark);

      &:hover {
        background-color: rgb(from var(--theme-base) r g b / 5%);
      }

      &:focus {
        outline: none;
      }
    }

    .submit-btn,
    .stop-btn {
      color: var(--query-editor-bg);
      background-color: var(--theme-base);
      width: 1.7rem;
      height: 1.7rem;
      display: flex;
      align-items: center;
      justify-content: center;
      border: none;
      border-radius: 9999px;
      cursor: pointer;

      &:disabled {
        opacity: 0.5;
      }

      span {
        font-variation-settings: "FILL" 1;
        font-size: 1rem;
      }
    }

    .stop-btn {
      &::after {
        content: "";
        background-color: currentcolor;
        opacity: 0.9;
        width: 0.7em;
        height: 0.7em;
      }
    }
  }
}

.preview-table-container {
  overflow-x: auto;
}

.preview-table {
  width: auto;
  border-collapse: collapse;
  margin: 0.5rem 0;
  table-layout: auto;

  th,
  td {
    border: 1px solid var(--border-color);
    padding: 0 0.7rem;
    text-align: left;
    white-space: nowrap;
    max-width: 200px;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  th {
    width: auto;
  }

  th {
    background-color: color-mix(in srgb, var(--theme-base) 5%, var(--query-editor-bg));
    font-weight: 600;
    font-size: 0.68rem;
    line-height: 1.7rem;
  }

  td {
    background-color: var(--query-editor-bg);
    font-size: 0.75rem;
    line-height: 1.875rem;
  }

  .remaining-rows td {
    font-style: italic;
    opacity: 0.7;
    padding-left: 1rem;
  }
}

.model-option.disabled {
  display: block;
  width: 100%;
  padding: 0.5rem 0.75rem;
  opacity: 0.6;
  cursor: not-allowed;
  color: var(--text-dark);
  font-size: 0.85rem;
}

.spinner-container {
  padding: 0rem 2rem 1rem;
}

@media (min-width: 900px) {
  .chat-container .scroll-container>.header {
    background-color: transparent;
  }

  .chat-messages {
    z-index: 1;
  }

  .message-content .code-block .actions {
    top: 1.9rem;
  }
}
