// 全局变量定义
$primary-color: #165DFF;
$secondary-color: #6B7280;
$success-color: #00B42A;
$warning-color: #FFAA00;
$error-color: #F53F3F;
$text-primary: #333333;
$text-secondary: #666666;
$text-tertiary: #999999;
$border-color: #E5E7EB;
$bg-light: #F9FAFB;
$bg-white: #FFFFFF;
$shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
$shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
$shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);

// 主容器
.api-integrations {
  padding: 24px;
  min-height: 100vh;
  background-color: $bg-light;
}

// 页面标题区域
.page-header {
  display: flex;
  flex-direction: column;
  margin-bottom: 24px;
  padding-bottom: 16px;
  border-bottom: 1px solid $border-color;

  .page-title {
    font-size: 24px;
    font-weight: 600;
    color: $text-primary;
    margin: 0 0 4px 0;
  }

  .page-description {
    font-size: 14px;
    color: $text-tertiary;
    margin: 0;
  }
}

// 标签页导航
.tab-navigation {
  display: flex;
  gap: 8px;
  margin-bottom: 24px;
  background-color: $bg-white;
  border-radius: 12px;
  padding: 4px;
  box-shadow: $shadow-sm;

  .tab-btn {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px 24px;
    border: none;
    background-color: transparent;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 500;
    color: $text-secondary;
    cursor: pointer;
    transition: all 0.2s ease;

    &:hover {
      background-color: $bg-light;
      color: $text-primary;
    }

    &.active {
      background-color: $primary-color;
      color: $bg-white;
    }
  }
}

// 标签页内容
.tab-content {
  animation: fadeIn 0.3s ease-in-out;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

// 搜索和筛选区域
.search-filter-section {
  display: flex;
  gap: 16px;
  margin-bottom: 24px;
  flex-wrap: wrap;

  .search-input-wrapper {
    flex: 1;
    min-width: 300px;
    position: relative;
    display: flex;
    align-items: center;
    background-color: $bg-white;
    border-radius: 8px;
    box-shadow: $shadow-sm;
    overflow: hidden;

    .search-icon {
      position: absolute;
      left: 16px;
      color: $text-tertiary;
      z-index: 1;
    }

    .search-input {
      flex: 1;
      padding: 12px 16px 12px 48px;
      border: none;
      background-color: transparent;
      font-size: 14px;
      color: $text-primary;
      outline: none;

      &::placeholder {
        color: $text-tertiary;
      }
    }

    .clear-search-btn {
      position: absolute;
      right: 16px;
      color: $text-tertiary;
      padding: 8px;
    }
  }

  .filter-controls {
    display: flex;
    gap: 12px;
    align-items: center;
    flex-wrap: wrap;

    .filter-wrapper {
      .filter-select {
        min-width: 160px;
        border: 1px solid $border-color;
        border-radius: 6px;
        background-color: $bg-white;
        box-shadow: $shadow-sm;

        .mat-select-trigger {
          height: 40px;
          font-size: 14px;
          color: $text-primary;
        }
      }
    }

    .date-filter-wrapper {
      .date-field {
        width: 140px;
        .mat-form-field-wrapper {
          .mat-form-field-flex {
            border-radius: 6px;
          }
          .mat-form-field-outline {
            border-radius: 6px;
          }
          .mat-form-field-infix {
            padding: 8px 0;
            .mat-input-element {
              font-size: 14px;
              color: $text-primary;
            }
          }
          .mat-form-field-label {
            font-size: 14px;
            color: $text-secondary;
          }
        }
      }
    }

    .filter-btn,
    .clear-filter-btn {
      display: flex;
      align-items: center;
      gap: 6px;
      color: $text-secondary;
      border: 1px solid $border-color;
      border-radius: 6px;
      padding: 8px 16px;
      font-size: 14px;
      font-weight: 500;
      background-color: $bg-white;
      transition: all 0.2s ease;

      &:hover {
        background-color: $bg-light;
        border-color: $text-tertiary;
        color: $text-primary;
      }
    }

    .create-btn,
    .export-btn {
      display: flex;
      align-items: center;
      gap: 8px;
      background-color: $primary-color;
      color: $bg-white;
      border-radius: 6px;
      padding: 8px 16px;
      font-size: 14px;
      font-weight: 500;
      transition: all 0.2s ease;
      box-shadow: $shadow-sm;

      &:hover:not(:disabled) {
        background-color: #0E4BD8;
        box-shadow: $shadow-md;
        transform: translateY(-1px);
      }

      &:disabled {
        opacity: 0.5;
        cursor: not-allowed;
        transform: none;
        box-shadow: none;
      }
    }
  }
}

// 统计卡片区域
.stats-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 16px;
  margin-bottom: 24px;

  .stat-card {
    background-color: $bg-white;
    border-radius: 12px;
    padding: 20px;
    box-shadow: $shadow-sm;
    text-align: center;
    transition: all 0.2s ease;

    &:hover {
      box-shadow: $shadow-md;
      transform: translateY(-2px);
    }

    .stat-value {
      font-size: 28px;
      font-weight: 700;
      color: $text-primary;
      margin-bottom: 4px;
    }

    .stat-label {
      font-size: 14px;
      color: $text-tertiary;
    }
  }
}

// API集成列表
.integrations-list {
  margin-bottom: 24px;
}

.integration-item {
  margin-bottom: 16px;

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

.integration-panel {
  background-color: $bg-white;
  border-radius: 12px;
  box-shadow: $shadow-sm;
  overflow: hidden;
  border: 1px solid $border-color;

  .mat-expansion-panel-header {
    padding: 0;
    height: auto;
    min-height: auto;
  }
}

.integration-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  background-color: $bg-white;
  cursor: pointer;
  transition: background-color 0.2s ease;

  &:hover {
    background-color: $bg-light;
  }

  .header-left {
    display: flex;
    align-items: center;
    gap: 16px;
    flex: 1;
  }

  .status-badge {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 4px 12px;
    border-radius: 16px;
    font-size: 12px;
    font-weight: 500;
    color: $bg-white;
    text-align: center;
    white-space: nowrap;

    .status-icon {
      font-size: 14px;
    }
  }

  .integration-info {
    .integration-name {
      font-size: 16px;
      font-weight: 600;
      color: $text-primary;
      margin: 0 0 8px 0;
    }

    .integration-meta {
      display: flex;
      gap: 16px;
      flex-wrap: wrap;

      .meta-item {
        display: flex;
        align-items: center;
        gap: 6px;
        font-size: 12px;
        color: $text-tertiary;

        &.type,
        &.auth {
          padding: 2px 8px;
          background-color: $bg-light;
          border-radius: 12px;
        }
      }
    }
  }

  .header-right {
    .action-buttons {
      display: flex;
      gap: 8px;

      .action-btn {
        width: 36px;
        height: 36px;
        border-radius: 6px;
        display: flex;
        align-items: center;
        justify-content: center;
        transition: all 0.2s ease;
        background-color: transparent;
        border: 1px solid $border-color;
        color: $text-secondary;

        &:hover {
          border-color: currentColor;
          transform: translateY(-1px);
        }

        &.mat-primary {
          color: $primary-color;
        }

        &.mat-warn {
          color: $error-color;
        }
      }
    }
  }
}

// 集成详情内容
.integration-content {
  padding: 20px;
  border-top: 1px solid $border-color;

  .detail-section {
    margin-bottom: 24px;

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

    &.actions {
      display: flex;
      justify-content: space-between;
      align-items: center;
      flex-wrap: wrap;
      gap: 16px;
      padding-top: 16px;
      border-top: 1px solid $border-color;
    }

    .section-title {
      font-size: 14px;
      font-weight: 600;
      color: $text-secondary;
      margin-bottom: 12px;
    }

    .detail-grid {
      display: grid;
      grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
      gap: 16px;

      .detail-item {
        display: flex;
        flex-direction: column;
        gap: 4px;

        label {
          font-size: 12px;
          color: $text-tertiary;
          font-weight: 500;
        }

        .detail-value {
          font-size: 14px;
          color: $text-primary;
          word-break: break-all;
        }
      }
    }

    .description-text {
      font-size: 14px;
      color: $text-primary;
      line-height: 1.5;
      margin: 0;
    }

    .configuration-details {
      display: grid;
      grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
      gap: 12px;

      .config-item {
        display: flex;
        flex-direction: column;
        gap: 4px;
        padding: 12px;
        background-color: $bg-light;
        border-radius: 6px;

        label {
          font-size: 12px;
          color: $text-secondary;
          font-weight: 500;
        }

        .config-value {
          font-size: 13px;
          color: $text-primary;
          font-family: 'Courier New', monospace;
          word-break: break-all;
        }
      }
    }

    .status-toggle-wrapper {
      .status-toggle {
        .mat-slide-toggle-content {
          font-size: 14px;
        }
      }
    }

    .test-btn {
      display: flex;
      align-items: center;
      gap: 6px;
      padding: 8px 16px;
      font-size: 14px;
    }
  }
}

// 日志表格容器
.logs-table-container {
  background-color: $bg-white;
  border-radius: 12px;
  box-shadow: $shadow-sm;
  overflow: hidden;
  margin-bottom: 24px;

  .logs-table {
    width: 100%;
    border-collapse: collapse;

    .table-header {
      background-color: $bg-light;
      font-weight: 600;
      color: $text-secondary;
      font-size: 13px;
      text-align: left;
      padding: 14px 16px;
      border-bottom: 1px solid $border-color;
      cursor: pointer;
      transition: background-color 0.2s ease;

      &:hover {
        background-color: #F3F4F6;
      }

      .header-content {
        display: flex;
        align-items: center;
        justify-content: space-between;
      }

      .sort-icon {
        opacity: 0.5;
      }
    }

    .table-cell {
      padding: 12px 16px;
      border-bottom: 1px solid $border-color;
      font-size: 13px;
      color: $text-primary;
      vertical-align: middle;

      .integration-name {
        font-weight: 500;
      }

      .method-badge {
        display: inline-block;
        padding: 4px 10px;
        background-color: $primary-color;
        color: $bg-white;
        border-radius: 12px;
        font-size: 11px;
        font-weight: 600;
        text-transform: uppercase;
      }

      .url-content {
        max-width: 250px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        font-family: 'Courier New', monospace;
      }

      .status-code-badge {
        display: inline-block;
        padding: 4px 10px;
        background-color: $border-color;
        color: $text-secondary;
        border-radius: 12px;
        font-size: 12px;
        font-weight: 600;
        font-family: 'Courier New', monospace;

        &.success {
          background-color: #E6F7EF;
          color: $success-color;
        }

        &.error {
          background-color: #FFF1F0;
          color: $error-color;
        }
      }

      .duration-value {
        font-family: 'Courier New', monospace;
        color: $text-secondary;
      }

      .log-status-badge {
        display: inline-block;
        padding: 4px 12px;
        border-radius: 16px;
        font-size: 12px;
        font-weight: 500;

        &.success {
          background-color: #E6F7EF;
          color: $success-color;
        }

        &.error {
          background-color: #FFF1F0;
          color: $error-color;
        }
      }

      .error-message {
        font-size: 12px;
        color: $error-color;
        margin-top: 4px;
        font-family: 'Courier New', monospace;
        word-break: break-all;
      }
    }

    tr:last-child .table-cell {
      border-bottom: none;
    }

    tr:hover .table-cell {
      background-color: $bg-light;
    }
  }

  // 无数据状态
  .empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 80px 20px;
    text-align: center;

    svg {
      margin-bottom: 16px;
      opacity: 0.5;
    }

    p {
      font-size: 16px;
      color: $text-tertiary;
      margin-bottom: 24px;
    }

    button {
      border-radius: 8px;
      padding: 8px 24px;
      font-size: 14px;
      font-weight: 500;
    }
  }
}

// 分页控件
.pagination-controls {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  background-color: $bg-white;
  border-radius: 12px;
  box-shadow: $shadow-sm;

  .pagination-info {
    font-size: 14px;
    color: $text-tertiary;
  }

  .pagination-buttons {
    display: flex;
    align-items: center;
    gap: 8px;

    button {
      padding: 6px 12px;
      border-radius: 6px;
      font-size: 13px;
      font-weight: 500;
      transition: all 0.2s ease;
      min-width: auto;

      &:disabled {
        opacity: 0.5;
        cursor: not-allowed;
      }

      &.active {
        background-color: $primary-color;
        color: $bg-white;
      }
    }

    .page-numbers {
      display: flex;
      gap: 4px;
    }
  }
}

// 响应式设计
@media (max-width: 1200px) {
  .search-filter-section {
    flex-direction: column;
    align-items: stretch;
  }

  .search-input-wrapper {
    min-width: 100% !important;
  }

  .logs-table {
    .table-header,
    .table-cell {
      padding: 12px;
    }
  }
}

@media (max-width: 992px) {
  .api-integrations {
    padding: 16px;
  }

  .filter-controls {
    flex-wrap: wrap;
  }

  .integration-header {
    flex-direction: column;
    align-items: stretch;
    gap: 16px;
  }

  .header-right {
    justify-content: flex-end;
  }

  .detail-grid,
  .configuration-details {
    grid-template-columns: 1fr !important;
  }

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

  .pagination-controls {
    flex-direction: column;
    gap: 16px;

    .pagination-buttons {
      flex-wrap: wrap;
      justify-content: center;
    }
  }

  .stats-cards {
    grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  }
}

@media (max-width: 768px) {
  .page-title {
    font-size: 20px !important;
  }

  .detail-section.actions {
    flex-direction: column;
    align-items: stretch !important;
  }

  .stat-card {
    padding: 16px;

    .stat-value {
      font-size: 24px;
    }
  }
}

@media (max-width: 480px) {
  .api-integrations {
    padding: 12px;
  }

  .tab-btn {
    padding: 10px 16px !important;
    font-size: 13px !important;
  }

  .create-btn,
  .export-btn {
    padding: 8px 16px !important;
    font-size: 13px !important;
    width: 100%;
    justify-content: center;
  }
}

// 滚动条样式
.integration-content,
.logs-table-container {
  &::-webkit-scrollbar {
    width: 6px;
  }

  &::-webkit-scrollbar-track {
    background: $bg-light;
    border-radius: 3px;
  }

  &::-webkit-scrollbar-thumb {
    background: $border-color;
    border-radius: 3px;
  }

  &::-webkit-scrollbar-thumb:hover {
    background: $text-tertiary;
  }
}