// ============================================================================
// Toolbar 工具栏样式
// ============================================================================

// Toolbar 吸顶样式（所有视图共用）
.ai-table-toolbar-sticky {
  position: sticky;
  z-index: 20;
  background: transparent; // 透明背景，使用父容器的渐变背景
}

.toolbar-container {
  display: flex;
  flex-direction: column;
  height: 34px;
}

.toolbar-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 8px;
  min-width: 0; // 允许 flex 子元素缩小
}

.toolbar-left {
  display: flex;
  align-items: center;
  padding: 0;
  gap: 8px;
  z-index: 1;
  flex: 1; // 占满剩余空间
  min-width: 0; // 允许缩小，超出时显示滚动条
  overflow-x: auto;
}

.toolbar-center-right {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 12px;
  flex-shrink: 0; // 防止被压缩
  min-width: 0; // 允许内容缩小
}

.toolbar-right {
  /* 自动布局 */
  display: flex;
  flex-direction: row;
  align-items: center;
  padding: 4px 4px;
  gap: 4px;
  box-sizing: border-box;
  flex-shrink: 1; // 允许收缩以适应空间
  min-width: 0; // 允许 flex 子元素缩小
  white-space: nowrap; // 防止内容换行
  border-radius: var(--cnai-radius-md, 8px);
  border: 1px solid var(--cnai-color-border-base, #e7e8ed);
}

.toolbar-actions {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 8px;
  flex-shrink: 0; // 防止被压缩
  white-space: nowrap; // 防止内容换行
}

.toolbar-action-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 4px 8px;
  min-width: 24px;
  height: 24px;
  color: #666;

  &:disabled {
    color: #ccc;
    cursor: not-allowed;
  }

  &:not(:disabled):hover {
    color: #333;
    background-color: #f5f5f5;
    border-radius: 4px;
  }
}

.toolbar-add-btn {
  display: flex;
  align-items: center;
  padding: 4px;
  gap: 4px;
  z-index: 3;
  border-radius: var(--cnai-radius-sm, 4px);
  background: var(--cnai-gradient-brand-light, linear-gradient(135deg, #d79bff 3%, #607aff 96%));
  transition: background 0.2s;

  &:hover,
  &.toolbar-add-btn-active {
    border-radius: var(--cnai-radius-sm, 4px);
    background: var(--cnai-gradient-brand-hover, linear-gradient(135deg, #a114ff 3%, #4161fd 96%));
  }
}

.add-record-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
}

.add-record-icon {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background-color: var(--cnai-color-brand-primary, #4161fd);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 14px;
  line-height: 1;
}

.toolbar-divider {
}

.toolbar-btn {
  display: flex;
  align-items: center;
  gap: 4px;
  width: auto !important;
  flex-shrink: 0; // 防止按钮被压缩
  white-space: nowrap; // 防止文字换行
  transition: transform 0.15s ease;

  &:active {
    transform: scale(0.92);
  }

  img {
    width: 16px;
    height: 16px;
    flex-shrink: 0; // 防止图标被压缩
  }

  span {
    color: var(--cnai-color-text-primary, #333);
  }
}

// 使用更高优先级的选择器确保按钮高度为 24px
.toolbar-container .toolbar-content .toolbar-center-right .toolbar-right .toolbar-btn,
.toolbar-container .toolbar-content .toolbar-center-right .toolbar-right .cn-ui-button.toolbar-btn {
  padding: 3px 8px;
  // 确保按钮宽度自适应内容
  min-width: auto;
  width: fit-content;
  border-radius: 4px;
}

// 响应式：当工具栏宽度不足时，隐藏文字，只显示图标
// 使用 JavaScript 动态添加的类名来控制
// 使用更具体的选择器提高优先级，避免使用 !important
.toolbar-container .toolbar-content .toolbar-center-right .toolbar-right-compact .toolbar-btn,
.toolbar-container
  .toolbar-content
  .toolbar-center-right
  .toolbar-right-compact
  .cn-ui-button.toolbar-btn {
  span {
    display: none;
  }
  // 只显示图标时，调整 padding 使其更紧凑
  padding: 3px;
  gap: 0;
  // 确保按钮宽度自适应，不保留空白
  width: auto;
  min-width: auto;
  max-width: none;
}

// 使用更具体的选择器提高优先级，确保悬浮样式生效
// 排除激活状态，激活状态下不显示悬浮背景色
.toolbar-container
  .toolbar-content
  .toolbar-center-right
  .toolbar-right
  .toolbar-btn:hover:not(.toolbar-btn-active),
.toolbar-container
  .toolbar-content
  .toolbar-center-right
  .toolbar-right
  .cn-ui-button.toolbar-btn:hover:not(.toolbar-btn-active) {
  border-radius: 4px;
  background: var(--cnai-color-bg-hover, #dedfe7);
  // 保持字体颜色不变
  color: inherit;

  // 确保内部元素颜色也不变
  span,
  img {
    color: inherit;
    opacity: 1;
  }
}

// mousedown 状态样式
.toolbar-container
  .toolbar-content
  .toolbar-center-right
  .toolbar-right
  .toolbar-btn:active:not(.toolbar-btn-active),
.toolbar-container
  .toolbar-content
  .toolbar-center-right
  .toolbar-right
  .cn-ui-button.toolbar-btn:active:not(.toolbar-btn-active) {
  border-radius: 4px;
  background: var(--cnai-color-bg-active, #f7f0fe);
  // 保持字体颜色不变
  color: inherit;

  // 确保内部元素颜色也不变
  span,
  img {
    color: inherit;
    opacity: 1;
  }
}

// 激活状态样式（有设置条件时）
// 使用更具体的选择器提高优先级，确保样式生效
.toolbar-container .toolbar-content .toolbar-center-right .toolbar-right .toolbar-btn-active,
.toolbar-container
  .toolbar-content
  .toolbar-center-right
  .toolbar-right
  .cn-ui-button.toolbar-btn-active {
  border-radius: 4px;
  background: var(--cnai-color-bg-active, #f7f0fe);
  color: inherit;

  span,
  img {
    color: inherit;
    opacity: 1;
  }

  &:hover {
    background: var(--cnai-color-bg-active, #f7f0fe);
  }

  &:active {
    background: var(--cnai-color-bg-active-deep, #ebe6ff);
  }
}

.popup-container {
  background-color: white;
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 16px;
  min-width: 200px;
}

.popup-title {
  margin-bottom: 12px;
  font-weight: bold;
}

.condition-popup {
  background: #fff;
  border-radius: 8px;
  width: 700px;
  max-width: 90vw;

  &.condition-popup-filter {
    width: 600px;
  }

  &.condition-popup-sort,
  &.condition-popup-group {
    width: 480px;
  }
  box-sizing: border-box;
  box-shadow: 0 4px 20px 0 rgba(0, 0, 0, 0.12);
  font-size: 12px;
}

.condition-header {
  height: 52px;
  display: flex;
  padding: 0 16px;
  gap: 8px;
  align-items: center;
  align-self: stretch;
  z-index: 0;
  font-size: 14px;
  font-weight: 500;
  line-height: 20px;
  min-width: 0;

  > span:first-child {
    flex: 1 1 auto;
    min-width: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    line-height: 20px;
  }
}

// 请注意提示消息 - 根据设计稿样式
.condition-popup .condition-header .condition-hint-message {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px 8px 4px 4px;
  background-color: var(--cnai-color-bg-selected-alt, #f0f5ff);
  border-radius: 8px;
  flex-shrink: 0;

  .hint-icon {
    color: var(--cnai-color-brand-primary, #4161fd);
    flex-shrink: 0;
    width: 16px;
    height: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .hint-text {
    color: #333; // token: color-neutural/text
    font-family: PingFangSC-Regular;
    font-size: 12px; // token: 12/lh18
    line-height: 18px;
    white-space: nowrap;

    .hint-text-bold {
      font-weight: 600; // 加粗
    }
  }
}

.condition-divider {
  height: 1px;
  background-color: #e8e8e8;
}

.condition-content {
  display: flex;
  flex-direction: column;
  padding: 12px 12px 16px 12px;
  gap: 8px;
  align-self: stretch;
  z-index: 2;
}

.condition-item {
  display: flex;
  align-items: center;
  padding: 2px 0;
  gap: 8px;
  align-self: stretch;
  z-index: 0;
  min-height: 32px;
}

/* 「且」占位：固定宽度保证多行拖拽手柄、字段列对齐 */
.condition-item-and-slot {
  flex-shrink: 0;
  width: 18px;
  font-size: 12px;
  color: #666;
  text-align: center;
  line-height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.drag-handle {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  // width: 24px;
  height: 24px;
  cursor: grab;
  color: #999;
  padding: 0;

  &:active {
    cursor: grabbing;
  }

  img {
    width: 16px;
    height: 16px;
    display: block;
    flex-shrink: 0;
  }
}

.condition-item-drag-overlay {
  background: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  opacity: 0.95;
}

.condition-popup .field-select,
.condition-popup .operator-select {
  font-size: 12px;
  min-height: 24px;
}

.condition-popup .condition-item .field-select,
.condition-popup .condition-item .operator-select {
  display: inline-flex;
  align-items: center;
}

.field-select {
  flex: 1;
  min-width: 120px;
  max-width: 300px;
}

.field-select-popup {
  .cn-next-menu-item,
  .cn-next-select-menu-item {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .cn-next-menu-item-text {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}

.field-option {
  display: flex;
  align-items: center;
  gap: 8px;
  overflow: hidden;

  img {
    width: 16px;
    height: 16px;
    flex-shrink: 0;
  }
}

.field-option-label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.operator-select {
  width: 90px;
  flex-shrink: 0;
}

.condition-popup .value-input {
  font-size: 12px;
}

.value-input {
  width: 200px;
  flex-shrink: 0;
}

.condition-item .delete-btn {
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  padding: 0;
  color: #ff4d4f;
  font-size: 12px;
  box-sizing: border-box;

  // 覆盖宿主页 cn-next-btn 可能将内部 img 设为 0x0 的样式，确保删除图标可见（如 caibao 域名下）
  img {
    width: 16px;
    height: 16px;
    display: block;
    flex-shrink: 0;
  }
}

.add-condition-container {
  display: flex;
  flex-direction: column;
  padding: 4px 0 0 0;
  align-self: stretch;
  z-index: 3;
}

.condition-popup .condition-content .add-condition-container .cn-ui-button.add-condition-btn {
  padding: 4px 8px;
  font-size: 12px;
  border: none;
  border-radius: 4px;
  transition:
    background 0.2s,
    transform 0.15s ease;
  background: transparent;

  &:hover {
    background: var(--cnai-color-bg-hover, #dedfe7);
    color: inherit;
  }

  &:active {
    transform: scale(0.95);
  }

  img {
    width: 16px;
    height: 16px;
    display: block;
    flex-shrink: 0;
  }
}

.add-condition-btn-disabled {
  color: #ccc;
  cursor: not-allowed;

  img {
    opacity: 0.4;
  }
}

.add-condition-icon {
  margin-right: 4px;
}

.add-condition-icon-disabled {
  color: #ccc;
}

.sort-icon-wrapper {
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.condition-popup .condition-item .sort-buttons-container {
  display: flex;
  align-items: center;
  padding: 2px;
  flex-grow: 1;
  align-self: stretch;
  z-index: 2;
  border-radius: 4px;
  background: rgba(0, 0, 0, 0.04);
  box-sizing: border-box;
  gap: 2px;
  max-width: 132px;

  // 排序按钮基础样式（覆盖 CnButton 默认样式）
  .cn-ui-button.sort-btn.cn-ui-button,
  .cn-ui-button.sort-btn {
    display: flex;
    justify-content: center;
    align-items: center;
    min-width: 60px;
    height: 24px;
    padding: 0 8px;
    gap: 4px;
    flex-shrink: 0;
    flex-grow: 0;
    box-sizing: border-box;
    border: none;
    z-index: 0;
  }

  // 未选中状态的悬浮样式
  .cn-ui-button.sort-btn.sort-btn-normal:hover:not(.sort-btn-asc):not(.sort-btn-desc),
  .cn-ui-button.sort-btn:hover:not(.sort-btn-asc):not(.sort-btn-desc) {
    background: rgba(0, 0, 0, 0.06);
    color: inherit;
    border-radius: 4px;
  }

  // 激活状态样式
  .cn-ui-button.sort-btn-asc,
  .cn-ui-button.sort-btn-desc {
    border-radius: 4px;
    background: #fff;
    box-shadow:
      0 2px 4px 0 rgba(0, 0, 0, 0.02),
      0 1px 6px -1px rgba(0, 0, 0, 0.02),
      0 1px 2px 0 rgba(0, 0, 0, 0.03);
    font-size: 12px;
    font-weight: normal;
    line-height: 18px;
    letter-spacing: normal;
    color: var(--cnai-color-brand-primary, #4161fd);
  }
}

.row-height-popup {
  background-color: white;
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 16px;
  min-width: 120px;
}

.row-height-icon-wrapper {
  display: flex;
  flex-direction: column;
}

.color-fill-popup {
  background-color: white;
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 16px;
  min-width: 500px;
  margin-top: 6px;
  // 添加阴影
  box-shadow: 0 4px 6px 2px rgba(0, 0, 0, 0.1);
}

.color-preview {
  width: 16px;
  height: 16px;
  display: inline-block;
  border-radius: 2px;
  border: 1px solid #ddd;
}

.color-picker-trigger {
  position: relative;
  width: 32px;
  height: 32px;
  border: 1px solid rgba(0, 0, 0, 0.05);
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.2s;
}

.color-picker-popup {
  border-radius: 8px;
  background: #fff;
  border-width: 1px 1px 1px 1px;
  border-style: solid;
  border-color: #e7e8ed;
  padding: 8px 8px 12px 8px;
}

.color-picker-grid {
  display: grid;
  grid-template-columns: auto repeat(6, 32px);
  gap: 8px;
  grid-auto-rows: 32px;
}

.color-picker-empty-cell {
  visibility: hidden;
}

.color-picker-header-cell {
  border-radius: 4px;
  background: rgba(0, 0, 0, 0.03);
  width: 32px;
  height: 32px;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 0;
  z-index: 4;
  font-size: 12px;
  color: #333;
  font-weight: 500;
  box-sizing: border-box;
}

.color-picker-data-row {
  display: contents;
}

.color-picker-row-header {
  border-radius: 4px;
  background: rgba(0, 0, 0, 0.03);
  width: 32px;
  height: 32px;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 0;
  z-index: 4;
  font-size: 12px;
  color: #333;
  font-weight: 500;
  box-sizing: border-box;
  border: none;
}

.color-row {
  display: flex;
  gap: 6px;
  margin-bottom: 6px;

  &:last-child {
    margin-bottom: 0;
  }
}

.color-swatch {
  width: 32px;
  height: 32px;
  border-radius: 4px;
  box-sizing: border-box;
  border: 1px solid rgba(0, 0, 0, 0.05);
  cursor: pointer;
  transition: all 0.2s;

  &:hover {
    transform: scale(1.05);
  }

  &.selected {
    border: 2px solid #1890ff;
    box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.2);
  }
}

.scope-select {
  width: 100px;
}

.field-select-color {
  flex: 1;
  min-width: 120px;
}

.smart-query-container {
  display: flex;
  align-items: center;
  gap: 8px;
  justify-content: flex-end;
}

.smart-query-wrapper {
  display: flex;
  align-items: center;
  border: 1px solid #1890ff;
  border-radius: 4px;
  background-color: #fff;
  padding: 0 8px;
}

.smart-query-icon {
  margin-right: 8px;
  color: #666;
}

.smart-query-input {
  border: none;
  box-shadow: none;
  flex: 1;
  width: 100%;
}

.smart-query-btn {
  display: flex;
  align-items: center;
  gap: 4px;
  background-color: #e6f7ff;
  border: 1px solid #91d5ff;
  color: #1890ff;
  border-radius: 4px;
  margin-left: 8px;
}

.smart-query-emoji {
  font-size: 16px;
  line-height: 1;
  color: #1890ff;
}

.smart-query-text {
  color: #1890ff;
}
