@use 'sass:color';
@use './variables.scss' as *;

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: $spacing-sm;
  border: 1px solid transparent;
  border-radius: $radius-md;
  padding: 10px 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all $transition-fast;
  background-color: var(--bg-secondary);
  color: var(--text-primary);

  &.btn-primary {
    background-color: var(--primary-color);
    color: #fff;
    border-color: var(--primary-color);

    &:hover {
      background-color: var(--primary-hover);
      border-color: var(--primary-hover);
    }
  }

  &.btn-secondary {
    background-color: var(--bg-tertiary);
    border-color: var(--border-color);
    color: var(--text-primary);

    &:hover {
      border-color: var(--border-hover);
    }
  }

  &.btn-ghost {
    background: transparent;
    border-color: transparent;
    color: var(--text-secondary);

    &:hover {
      color: var(--text-primary);
      background: var(--bg-secondary);
    }
  }

  &.btn-danger {
    background-color: $error-color;
    border-color: $error-color;
    color: #fff;

    &:hover {
      background-color: color.adjust($error-color, $lightness: -5%);
    }
  }

  &.btn-full {
    width: 100%;
  }

  &.btn-sm {
    padding: 8px 10px;
    font-size: 14px;
  }

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

.input,
textarea {
  width: 100%;
  border: 1px solid var(--border-color);
  border-radius: $radius-md;
  padding: 10px 12px;
  background-color: var(--bg-primary);
  color: var(--text-primary);
  transition: border-color $transition-fast, box-shadow $transition-fast;

  &:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.15);
  }
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: $spacing-xs;
  margin-bottom: $spacing-md;

  label {
    font-weight: 600;
    color: var(--text-primary);
  }

  .hint {
    color: var(--text-secondary);
    font-size: 13px;
  }
}

.card {
  background-color: var(--bg-primary);
  border: 1px solid var(--border-color);
  border-radius: $radius-lg;
  box-shadow: var(--shadow);
  padding: $spacing-lg;
}

.card-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  margin-bottom: $spacing-md;
  gap: 12px;
  flex-wrap: wrap;

  @media (max-width: 768px) {
    flex-direction: column;
    gap: 10px;
  }

  .card-title-group {
    display: flex;
    flex-direction: column;
    gap: 4px;
    min-width: 0;
    flex: 1;
  }

  .title {
    font-size: 18px;
    font-weight: 700;
    color: var(--text-primary);

    @media (max-width: 768px) {
      font-size: 16px;
    }
  }

  .subtitle {
    font-size: 13px;
    color: var(--text-secondary);

    @media (max-width: 768px) {
      font-size: 12px;
    }
  }
}

.status-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  border-radius: $radius-full;
  font-size: 13px;
  border: 1px solid var(--border-color);
  margin-bottom: $spacing-md;

  // 确保后续内容换行显示
  + * {
    display: block;
  }

  &.success {
    color: $success-color;
    border-color: rgba(16, 185, 129, 0.35);
    background: rgba(16, 185, 129, 0.08);
  }

  &.warning {
    color: $warning-color;
    border-color: rgba(245, 158, 11, 0.35);
    background: rgba(245, 158, 11, 0.08);
  }

  &.error {
    color: $error-color;
    border-color: rgba(239, 68, 68, 0.35);
    background: rgba(239, 68, 68, 0.08);
  }

  &.muted {
    color: var(--text-secondary);
  }
}

.notification-container {
  position: fixed;
  top: $spacing-lg;
  right: $spacing-lg;
  display: flex;
  flex-direction: column;
  gap: $spacing-sm;
  z-index: $z-notification;
  max-width: 360px;
}

@keyframes notification-enter {
  from {
    opacity: 0;
    transform: translateX(100%);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

@keyframes notification-exit {
  from {
    opacity: 1;
    transform: translateX(0);
  }
  to {
    opacity: 0;
    transform: translateX(100%);
  }
}

.notification {
  padding: $spacing-md;
  border-radius: $radius-md;
  box-shadow: var(--shadow);
  border: 1px solid var(--border-color);
  background: var(--bg-primary);
  color: var(--text-primary);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: $spacing-sm;

  // Animation states
  &.entering {
    animation: notification-enter 0.3s ease-out forwards;
  }

  &.exiting {
    animation: notification-exit 0.3s ease-in forwards;
  }

  &.success {
    border-color: rgba(16, 185, 129, 0.4);
  }
  &.warning {
    border-color: rgba(245, 158, 11, 0.4);
  }
  &.error {
    border-color: rgba(239, 68, 68, 0.4);
  }

  .message {
    flex: 1;
    font-weight: 500;
  }

  .close-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 30px;
    height: 30px;
    padding: 0;
    background: transparent;
    border: none;
    color: var(--text-secondary);
    border-radius: $radius-md;
    cursor: pointer;
    transition: color 0.15s ease, background-color 0.15s ease;

    svg {
      display: block;
    }

    &:hover {
      color: var(--text-primary);
      background: var(--bg-secondary);
    }
  }
}

.switch {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: $spacing-sm;
  cursor: pointer;

  input {
    width: 0;
    height: 0;
    opacity: 0;
    position: absolute;
  }

  .track {
    width: 44px;
    height: 24px;
    background: var(--border-color);
    border-radius: $radius-full;
    position: relative;
    transition: background $transition-fast;
  }

  .thumb {
    position: absolute;
    top: 3px;
    left: 3px;
    width: 18px;
    height: 18px;
    background: #fff;
    border-radius: $radius-full;
    box-shadow: $shadow-sm;
    transition: transform $transition-fast;
  }

  input:checked + .track {
    background: var(--primary-color);
  }

  input:checked + .track .thumb {
    transform: translateX(20px);
  }

  .label {
    color: var(--text-primary);
    font-weight: 600;
  }
}

.switch-label-left {
  .label {
    order: -1;
  }
}

.pill {
  padding: 4px 10px;
  border-radius: $radius-full;
  background: var(--bg-secondary);
  color: var(--text-secondary);
  border: 1px solid var(--border-color);
  font-size: 12px;
}

.loading-spinner {
  border: 3px solid rgba(255, 255, 255, 0.2);
  border-top-color: #fff;
  border-radius: 50%;
  width: 16px;
  height: 16px;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: $z-modal;
  padding: $spacing-lg;

  &.modal-overlay-entering {
    animation: modal-overlay-fade-in 0.25s ease-out forwards;
  }

  &.modal-overlay-closing {
    animation: modal-overlay-fade-out 0.35s ease-in forwards;
  }
}

@keyframes modal-overlay-fade-in {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes modal-overlay-fade-out {
  from {
    opacity: 1;
  }
  to {
    opacity: 0;
  }
}

.modal {
  background: var(--bg-primary);
  border-radius: $radius-lg;
  border: 1px solid var(--border-color);
  box-shadow: $shadow-lg;
  max-height: 90vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  position: relative;
  // 关闭按钮中心位置: right 12px + 16px = 28px, top 12px + 16px = 28px
  transform-origin: calc(100% - 28px) 28px;

  &.modal-entering {
    animation: modal-scale-in 0.3s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
  }

  &.modal-closing {
    animation: modal-collapse-to-close 0.35s cubic-bezier(0.4, 0, 0.2, 1) forwards;
  }
}

@keyframes modal-scale-in {
  from {
    opacity: 0;
    transform: scale(0.85) translateY(20px);
  }
  to {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
}

@keyframes modal-collapse-to-close {
  0% {
    opacity: 1;
    transform: scale(1);
  }
  100% {
    opacity: 0;
    transform: scale(0);
  }
}

.modal-close-floating {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 32px;
  height: 32px;
  padding: 0;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  color: var(--text-secondary);
  cursor: pointer;
  border-radius: $radius-full;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: color 0.15s ease, background-color 0.15s ease, transform 0.15s ease;
  z-index: 10;

  svg {
    display: block;
  }

  &:hover {
    color: var(--text-primary);
    background: var(--bg-tertiary);
    transform: scale(1.1);
  }

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

  &:disabled {
    cursor: not-allowed;
    opacity: 0.6;
    transform: none;
  }

  &:disabled:hover {
    color: var(--text-secondary);
    background: var(--bg-secondary);
    transform: none;
  }
}

.modal-header {
  display: flex;
  align-items: center;
  padding: $spacing-md $spacing-lg;
  border-bottom: 1px solid var(--border-color);

  .modal-title {
    font-weight: 700;
    font-size: 18px;
    color: var(--text-primary);
  }
}

.modal-body {
  padding: $spacing-lg;
  overflow: auto;
  max-height: 65vh;
}

.modal-footer {
  padding: $spacing-md $spacing-lg;
  border-top: 1px solid var(--border-color);
  display: flex;
  justify-content: flex-end;
  gap: $spacing-sm;
  background: var(--bg-primary);
}

.request-log-modal {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: $spacing-md;

  .status-badge {
    margin-bottom: 0;
  }
}

.empty-state {
  border: 1px dashed var(--border-color);
  border-radius: $radius-lg;
  padding: $spacing-lg;
  background: var(--bg-secondary);
  display: flex;
  justify-content: space-between;
  gap: $spacing-md;
  align-items: center;

  .empty-content {
    display: flex;
    align-items: center;
    gap: $spacing-md;
  }

  .empty-icon {
    width: 42px;
    height: 42px;
    border-radius: $radius-full;
    border: 2px solid var(--border-color);
    display: grid;
    place-items: center;
    color: var(--text-secondary);

    svg {
      display: block;
    }
  }

  .empty-title {
    font-weight: 700;
    color: var(--text-primary);
  }

  .empty-desc {
    color: var(--text-secondary);
    margin-top: 4px;
  }
}

.header-input-list {
  display: flex;
  flex-direction: column;
  gap: $spacing-sm;

  .header-input-row {
    display: grid;
    grid-template-columns: 1fr auto 1fr auto;
    align-items: center;
    gap: $spacing-sm;
  }

  .header-separator {
    color: var(--text-secondary);
    text-align: center;
  }

  .align-start {
    width: fit-content;
  }
}

.item-list {
  display: flex;
  flex-direction: column;
  gap: $spacing-sm;
}

.item-row {
  border: 1px solid var(--border-color);
  border-radius: $radius-md;
  padding: $spacing-md;
  background: var(--bg-primary);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: $spacing-md;
  flex-wrap: wrap;

  .item-meta {
    display: flex;
    flex-direction: column;
    gap: 6px;
  }

  .item-title {
    font-weight: 700;
    color: var(--text-primary);
  }

  .item-subtitle {
    color: var(--text-secondary);
    font-family: ui-monospace, SFMono-Regular, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New',
      monospace;
    word-break: break-all;
  }

  .item-actions {
    display: flex;
    gap: $spacing-sm;
  }
}

.error-box {
  background: rgba(239, 68, 68, 0.1);
  border: 1px solid rgba(239, 68, 68, 0.4);
  border-radius: $radius-md;
  padding: $spacing-sm $spacing-md;
  color: $error-color;
}

.stack {
  display: flex;
  flex-direction: column;
  gap: $spacing-lg;
}

.filters {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: $spacing-md;
  margin-bottom: $spacing-md;

  .filter-item {
    display: flex;
    flex-direction: column;
    gap: $spacing-xs;
  }
}

.table {
  display: flex;
  flex-direction: column;
  border: 1px solid var(--border-color);
  border-radius: $radius-md;
  overflow: hidden;

  .table-header,
  .table-row {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
    gap: $spacing-sm;
    padding: $spacing-sm $spacing-md;
    align-items: center;
  }

  .table-header {
    background: var(--bg-secondary);
    font-weight: 700;
    color: var(--text-primary);
  }

  .table-row {
    border-top: 1px solid var(--border-color);
  }

  .cell {
    display: flex;
    align-items: center;
    gap: $spacing-sm;
    flex-wrap: wrap;
  }
}

.pagination {
  display: flex;
  align-items: center;
  gap: $spacing-sm;
  margin-top: $spacing-md;
}

.stat-card {
  border: 1px solid var(--border-color);
  border-radius: $radius-md;
  padding: $spacing-md;
  background: var(--bg-primary);
  display: flex;
  flex-direction: column;
  gap: $spacing-xs;

  .stat-label {
    color: var(--text-secondary);
    font-size: 14px;
  }

  .stat-value {
    font-weight: 800;
    color: var(--text-primary);
    font-size: 18px;
  }
}

.log-viewer {
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: $radius-md;
  padding: $spacing-md;
  max-height: 520px;
  overflow: auto;
  white-space: pre-wrap;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace;
  color: var(--text-primary);
}

.log-viewer-lines {
  .log-line {
    display: block;
    padding: 1px 0;
  }

  .log-line-warning {
    color: var(--warning-text, #92400e);
    background: var(--warning-bg, rgba(251, 191, 36, 0.18));
    border-radius: 4px;
    padding: 2px 6px;
  }
}

.hint {
  color: var(--text-secondary);
}
