.contacts-page {
  display: flex;
  height: calc(100% + 48px);
  margin: -24px;
  background: var(--bg-primary);
  overflow: hidden;

  // 左侧联系人面板
  .contacts-panel {
    width: 324px;
    min-width: 324px;
    display: flex;
    flex-direction: column;
    border-right: 1px solid var(--border-color);
    background: var(--card-bg);
  }

  .panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 18px;
    border-bottom: 1px solid var(--border-color);

    h2 {
      font-size: 18px;
      font-weight: 600;
      color: var(--text-primary);
      margin: 0;
    }

    .icon-btn {
      width: 32px;
      height: 32px;
      border: none;
      background: var(--bg-tertiary);
      border-radius: 8px;
      cursor: pointer;
      display: flex;
      align-items: center;
      justify-content: center;
      color: var(--text-secondary);
      transition: background 0.15s ease, color 0.15s ease;

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

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

      .spin {
        animation: contactsSpin 1s linear infinite;
      }

      &.export-mode-btn.active {
        background: var(--primary);
        color: #fff;
      }
    }
  }

  .search-bar {
    display: flex;
    align-items: center;
    gap: 10px;
    margin: 12px 16px;
    padding: 8px 10px;
    background: var(--bg-secondary);
    border-radius: 8px;
    border: 1px solid var(--border-color);
    transition: border-color 0.2s;

    &:focus-within {
      border-color: var(--primary);
    }

    svg {
      color: var(--text-tertiary);
      flex-shrink: 0;
    }

    input {
      flex: 1;
      border: none;
      background: none;
      outline: none;
      font-size: 14px;
      color: var(--text-primary);

      &::placeholder {
        color: var(--text-tertiary);
      }
    }

    .clear-btn {
      background: none;
      border: none;
      padding: 4px;
      cursor: pointer;
      color: var(--text-tertiary);
      display: flex;
      align-items: center;
      justify-content: center;
      border-radius: 4px;

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

  .type-filters {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px;
    padding: 0 16px 12px;
    max-width: 292px;

    &::-webkit-scrollbar {
      display: none;
    }

    .filter-chip {
      display: flex;
      align-items: center;
      gap: 6px;
      padding: 7px 10px;
      background: var(--bg-secondary);
      border: 1px solid var(--border-color);
      border-radius: 8px;
      cursor: pointer;
      user-select: none;
      font-size: 13px;
      font-weight: 500;
      color: var(--text-secondary);
      transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease;
      white-space: nowrap;

      input[type="checkbox"] {
        display: none;
      }

      svg {
        opacity: 0.7;
        transition: transform 0.2s;
        flex-shrink: 0;
      }

      .chip-label {
        min-width: 0;
      }

      .chip-count {
        margin-left: auto;
        text-align: right;
        font-variant-numeric: tabular-nums;
      }

      &:hover {
        background: var(--bg-hover);
        border-color: var(--text-tertiary);
        color: var(--text-primary);

        svg {
          transform: none;
        }
      }

      &.active {
        background: var(--primary-light);
        border-color: var(--primary);
        color: var(--primary);

        svg {
          opacity: 1;
          color: var(--primary);
        }
      }
    }
  }

  .contacts-count {
    padding: 0 16px 10px;
    font-size: 13px;
    color: var(--text-secondary);

    .contacts-cache-meta {
      margin-left: 10px;
      color: var(--text-tertiary);
      font-size: 12px;

      &.syncing {
        color: var(--primary);
      }
    }

    .avatar-enrich-progress {
      margin-left: 10px;
      color: var(--text-tertiary);
      font-size: 12px;
    }
  }

  .selection-toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    padding: 0 16px 10px;

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

    .selection-count {
      font-size: 12px;
      color: var(--text-tertiary);
    }
  }

  .loading-state,
  .empty-state {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 12px;
    color: var(--text-tertiary);
    font-size: 14px;

    .spin {
      animation: contactsSpin 1s linear infinite;
    }
  }

  .load-issue-state {
    flex: 1;
    padding: 14px 14px 18px;
    overflow-y: auto;
  }

  .issue-card {
    border: 1px solid color-mix(in srgb, var(--danger, #ef4444) 45%, var(--border-color));
    background: color-mix(in srgb, var(--danger, #ef4444) 8%, var(--card-bg));
    border-radius: 8px;
    padding: 14px;
    color: var(--text-primary);

    .issue-title {
      display: flex;
      align-items: center;
      gap: 8px;
      font-size: 14px;
      font-weight: 600;
      color: color-mix(in srgb, var(--danger, #ef4444) 85%, var(--text-primary));
      margin-bottom: 8px;
    }

    .issue-message {
      margin: 0 0 8px;
      font-size: 13px;
      color: var(--text-secondary);
      line-height: 1.5;
    }

    .issue-reason {
      margin: 0;
      font-size: 13px;
      color: var(--text-secondary);
      line-height: 1.5;
    }

    .issue-hints {
      margin: 10px 0 0;
      padding-left: 18px;
      font-size: 12px;
      color: var(--text-tertiary);
      line-height: 1.6;
    }

    .issue-actions {
      margin-top: 12px;
      display: flex;
      flex-wrap: wrap;
      gap: 8px;
    }

    .issue-btn {
      border: 1px solid var(--border-color);
      background: var(--bg-secondary);
      border-radius: 8px;
      padding: 7px 10px;
      font-size: 12px;
      color: var(--text-secondary);
      display: inline-flex;
      align-items: center;
      gap: 6px;
      cursor: pointer;
      transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease;

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

      &.primary {
        background: color-mix(in srgb, var(--primary) 14%, var(--bg-secondary));
        border-color: color-mix(in srgb, var(--primary) 42%, var(--border-color));
        color: var(--primary);
      }
    }

    .issue-diagnostics {
      margin-top: 12px;
      border-radius: 8px;
      background: var(--bg-primary);
      border: 1px dashed var(--border-color);
      padding: 10px;
      font-size: 12px;
      line-height: 1.5;
      color: var(--text-secondary);
      white-space: pre-wrap;
      word-break: break-word;
    }
  }

  .contacts-list {
    flex: 1;
    overflow-y: auto;
    padding: 0 10px 10px;
    position: relative;

    &::-webkit-scrollbar {
      width: 6px;
    }

    &::-webkit-scrollbar-thumb {
      background: var(--text-tertiary);
      border-radius: 3px;
      opacity: 0.3;
    }
  }

  .contacts-list-virtual {
    position: relative;
    min-height: 100%;
  }

  .contact-row {
    position: absolute;
    left: 0;
    right: 0;
    height: 64px;
    padding-bottom: 4px;
    will-change: transform;
  }

  .contact-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 10px;
    height: 60px;
    box-sizing: border-box;
    border-radius: 8px;
    transition: background 0.15s ease;
    cursor: pointer;
    margin-bottom: 0;

    &:hover {
      background: var(--bg-hover);
    }

    &.selected {
      background: color-mix(in srgb, var(--primary) 12%, transparent);
    }

    &.active {
      background: var(--bg-tertiary);
    }

    .contact-select {
      display: flex;
      align-items: center;
      justify-content: center;
      flex-shrink: 0;

      input[type="checkbox"] {
        width: 16px;
        height: 16px;
        cursor: pointer;
        accent-color: var(--primary);
      }
    }

    .contact-avatar {
      width: 38px;
      height: 38px;
      border-radius: 8px;
      background: var(--bg-tertiary);
      display: flex;
      align-items: center;
      justify-content: center;
      overflow: hidden;
      flex-shrink: 0;

      img {
        width: 100%;
        height: 100%;
        object-fit: cover;
      }

      span {
        color: var(--text-secondary);
        font-size: 14px;
        font-weight: 600;
      }
    }

    .contact-info {
      flex: 1;
      min-width: 0;
    }

    .contact-name {
      font-size: 14px;
      font-weight: 500;
      color: var(--text-primary);
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }

    .contact-remark {
      font-size: 12px;
      color: var(--text-tertiary);
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      margin-top: 2px;
    }

    .contact-type {
      display: flex;
      align-items: center;
      gap: 4px;
      padding: 4px 10px;
      border-radius: 6px;
      font-size: 12px;
      font-weight: 500;
      flex-shrink: 0;

      &.friend {
        background: rgba(var(--primary-rgb), 0.1);
        color: var(--primary);
      }

      &.group {
        background: rgba(52, 211, 153, 0.1);
        color: rgb(52, 211, 153);
      }

      &.official {
        background: rgba(251, 191, 36, 0.1);
        color: rgb(251, 191, 36);
      }

      svg {
        flex-shrink: 0;
      }
    }
  }

  // 右侧详情面板内的联系人资料
  .detail-profile {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
    padding-bottom: 18px;
    border-bottom: 1px solid var(--border-color);
    margin-bottom: 16px;

    .detail-avatar {
      width: 68px;
      height: 68px;
      border-radius: 8px;
      background: var(--bg-tertiary);
      display: flex;
      align-items: center;
      justify-content: center;
      overflow: hidden;

      img { width: 100%; height: 100%; object-fit: cover; }
      span { color: var(--text-secondary); font-size: 24px; font-weight: 600; }
    }

    .detail-name {
      font-size: 18px;
      font-weight: 600;
      color: var(--text-primary);
    }
  }

  .detail-info-list {
    margin-bottom: 18px;

    .detail-row {
      display: flex;
      align-items: center;
      gap: 12px;
      padding: 10px 0;
      font-size: 13px;
      border-bottom: 1px solid var(--border-color);

      &:last-child { border-bottom: none; }
    }

    .detail-label {
      color: var(--text-tertiary);
      min-width: 48px;
      flex-shrink: 0;
    }

    .detail-value {
      color: var(--text-primary);
      word-break: break-all;
      user-select: text;
    }

    .detail-entry-btn {
      display: inline-flex;
      align-items: center;
      gap: 6px;
      margin-left: auto;
      border: 1px solid var(--border-color);
      border-radius: 8px;
      background: var(--bg-secondary);
      color: var(--text-primary);
      padding: 6px 10px;
      font-size: 12px;
      line-height: 1;
      cursor: pointer;
      transition: border-color 0.2s ease, color 0.2s ease, background 0.2s ease;

      &:hover {
        color: var(--primary);
        border-color: color-mix(in srgb, var(--primary) 45%, var(--border-color));
        background: color-mix(in srgb, var(--primary) 8%, var(--bg-secondary));
      }
    }
  }

  .goto-chat-btn {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 10px 12px;
    background: var(--primary);
    color: #fff;
    border: none;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.15s ease;

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

  .empty-detail {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 12px;
    color: var(--text-tertiary);
    font-size: 14px;
  }

  // 右侧设置面板
  .settings-panel {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }

  .settings-content {
    flex: 1;
    overflow-y: auto;
    padding: 16px 18px;

    &::-webkit-scrollbar {
      width: 6px;
    }

    &::-webkit-scrollbar-thumb {
      background: var(--text-tertiary);
      border-radius: 3px;
    }
  }

  .setting-section {
    margin-bottom: 22px;

    h3 {
      font-size: 13px;
      font-weight: 600;
      color: var(--text-secondary);
      text-transform: uppercase;
      letter-spacing: 0.5px;
      margin: 0 0 10px;
    }
  }

  .format-select {
    position: relative;
    /* margin-bottom 移到 .setting-section */

    .select-trigger {
      width: 100%;
      padding: 9px 12px;
      border: 1px solid var(--border-color);
      border-radius: 8px;
      font-size: 14px;
      background: var(--bg-primary);
      color: var(--text-primary);
      display: flex;
      align-items: center;
      justify-content: space-between;
      gap: 8px;
      cursor: pointer;
      transition: border-color 0.15s ease, box-shadow 0.15s ease;

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

      &.open {
        border-color: var(--primary);
        box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 15%, transparent);
      }
    }

    .select-value {
      flex: 1;
      min-width: 0;
      text-align: left;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }

    .select-dropdown {
      position: absolute;
      top: calc(100% + 6px);
      left: 0;
      right: 0;
      background: color-mix(in srgb, var(--bg-primary) 85%, var(--bg-secondary));
      border: 1px solid var(--border-color);
      border-radius: 8px;
      padding: 6px;
      box-shadow: 0 12px 28px rgba(0, 0, 0, 0.12);
      z-index: 20;
      max-height: 260px;
      overflow-y: auto;
    }

    .select-option {
      width: 100%;
      text-align: left;
      display: flex;
      flex-direction: column;
      gap: 4px;
      padding: 10px 12px;
      border: none;
      border-radius: 8px;
      background: transparent;
      cursor: pointer;
      transition: background 0.15s ease, color 0.15s ease;
      color: var(--text-primary);
      font-size: 14px;

      &:hover {
        background: var(--bg-tertiary);
      }

      &.active {
        background: color-mix(in srgb, var(--primary) 12%, transparent);
        color: var(--primary);

        .option-desc {
          color: var(--primary);
        }
      }
    }

    .option-label {
      font-weight: 500;
    }

    .option-desc {
      font-size: 12px;
      color: var(--text-tertiary);
    }
  }

  .checkbox-item {
    display: flex;
    align-items: center;
    gap: 10px;
    cursor: pointer;
    font-size: 14px;
    color: var(--text-primary);

    input[type="checkbox"] {
      width: 18px;
      height: 18px;
      accent-color: var(--primary);
      cursor: pointer;
    }
  }

  .export-path-display {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    background: var(--bg-secondary);
    border-radius: 8px;
    font-size: 13px;
    color: var(--text-primary);
    margin-bottom: 12px;

    svg {
      color: var(--primary);
      flex-shrink: 0;
    }

    span {
      flex: 1;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
  }

  .select-folder-btn {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 10px 16px;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    font-size: 13px;
    font-weight: 500;
    color: var(--text-primary);
    cursor: pointer;
    transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease;

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

      svg {
        color: var(--primary);
      }
    }

    svg {
      color: var(--text-secondary);
      transition: color 0.2s;
    }
  }

  .export-action {
    padding: 16px 18px;
    border-top: 1px solid var(--border-color);
  }

  .export-btn {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    padding: 12px 16px;
    background: var(--primary);
    color: #fff;
    border: none;
    border-radius: 8px;
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.15s ease;

    &:hover:not(:disabled) {
      background: var(--primary-hover);
    }

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

    .spin {
      animation: contactsSpin 1s linear infinite;
    }
  }
}

@keyframes contactsSpin {
  from {
    transform: rotate(0deg);
  }

  to {
    transform: rotate(360deg);
  }
}

// UI rebuild polish for the contact detail surface.
.contacts-page {
  background: var(--bg-primary);

  .contact-detail-panel {
    background: var(--bg-primary);
  }

  .contact-detail-scroll {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    padding: 24px;
    display: flex;
    flex-direction: column;
    gap: 16px;

    &::-webkit-scrollbar {
      width: 6px;
    }

    &::-webkit-scrollbar-thumb {
      background: color-mix(in srgb, var(--text-tertiary) 55%, transparent);
      border-radius: 3px;
    }
  }

  .contact-detail-hero {
    display: flex;
    align-items: center;
    gap: 18px;
    padding: 22px;
    border-radius: 16px;
    background: var(--bg-secondary);
    border: 1px solid color-mix(in srgb, var(--border-color) 72%, transparent);
  }

  .contact-detail-hero .detail-avatar {
    width: 84px;
    height: 84px;
    border-radius: 16px;
    background: var(--bg-tertiary);
    overflow: hidden;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;

    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }

    span {
      color: var(--text-secondary);
      font-size: 28px;
      font-weight: 650;
    }
  }

  .contact-detail-heading {
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 8px;

    h2 {
      margin: 0;
      color: var(--text-primary);
      font-size: 24px;
      line-height: 1.2;
      font-weight: 650;
      overflow-wrap: anywhere;
    }

    p {
      margin: 0;
      color: var(--text-secondary);
      font-size: 13px;
      line-height: 1.5;
      overflow-wrap: anywhere;
    }
  }

  .detail-type {
    width: fit-content;
    border-radius: 999px;
    padding: 5px 10px;
  }

  .contact-action-row {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 10px;
  }

  .contact-action-row .goto-chat-btn,
  .contact-action-row .detail-entry-btn {
    min-height: 44px;
    width: 100%;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    border-radius: 12px;
    padding: 10px 14px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease;
  }

  .contact-action-row .goto-chat-btn {
    border: 1px solid var(--primary);
    background: var(--primary);
    color: var(--on-primary, #fff);

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

  .contact-action-row .detail-entry-btn {
    border: 1px solid var(--border-color);
    background: var(--bg-secondary);
    color: var(--text-primary);

    &:hover {
      color: var(--primary);
      border-color: color-mix(in srgb, var(--primary) 40%, var(--border-color));
      background: color-mix(in srgb, var(--primary) 7%, var(--bg-secondary));
    }
  }

  .contact-detail-section {
    padding: 18px;
    border-radius: 16px;
    background: var(--bg-secondary);
    border: 1px solid color-mix(in srgb, var(--border-color) 72%, transparent);
  }

  .section-title {
    margin-bottom: 12px;
    color: var(--text-secondary);
    font-size: 12px;
    font-weight: 650;
  }

  .contact-detail-section .detail-info-list {
    margin: 0;
    display: grid;
    gap: 8px;
  }

  .contact-detail-section .detail-row {
    display: grid;
    grid-template-columns: 76px minmax(0, 1fr);
    align-items: start;
    gap: 12px;
    padding: 12px 14px;
    border: none;
    border-radius: 12px;
    background: var(--bg-primary);
    font-size: 13px;
  }

  .contact-detail-section .detail-label {
    min-width: 0;
    color: var(--text-tertiary);
  }

  .contact-detail-section .detail-value {
    color: var(--text-primary);
    line-height: 1.55;
    word-break: break-word;
    overflow-wrap: anywhere;
    user-select: text;
  }

  @media (max-width: 860px) {
    .contact-detail-hero {
      align-items: flex-start;
      flex-direction: column;
    }
  }
}
