<script setup lang="ts">
import { computed, onMounted, reactive, ref } from "vue";
import { useRouter, useRoute } from "vue-router";
import { useSessionStore } from "@/stores/session";
import { showDialog, showToast } from "vant";
import MobileBottomNav from "@/components/MobileBottomNav.vue";

import { fetchWalletBalance, fetchWalletLedger, type WalletLedger } from "@/features/wallet/api/wallet";
import { fetchPostRentalNotice, type PostRentalNotice } from "@/features/orders/api/orders";
import { formatDateMinute } from "@/utils/time";
import { uploadFile } from "@/shared/api/files";

const session = useSessionStore();
const router = useRouter();
const route = useRoute();

/** 数据项 */
const balance = ref(0);
const showSettings = ref(false);
const showProfileEditor = ref(false);
const savingProfile = ref(false);
const profileForm = reactive({
  nickname: "",
  avatar_url: "",
});

const avatarFileInput = ref<HTMLInputElement | null>(null);
const uploadingAvatar = ref(false);

function triggerAvatarUpload() {
  avatarFileInput.value?.click();
}

async function handleAvatarFileChange(event: Event) {
  const input = event.target as HTMLInputElement;
  const file = input.files?.[0];
  input.value = "";
  if (!file) return;

  uploadingAvatar.value = true;
  const toast = showToast({
    type: "loading",
    message: "上传中...",
    forbidClick: true,
    duration: 0,
  });

  try {
    const uploaded = await uploadFile(file, "avatar");
    profileForm.avatar_url = uploaded.url;
    showToast({ message: "上传成功", icon: "passed" });
  } catch (error) {
    showToast({ message: readError(error, "上传失败"), icon: "cross" });
  } finally {
    uploadingAvatar.value = false;
  }
}

// 收支明细列表
const showLedgers = ref(false);
const loadingLedgers = ref(false);
const ledgers = ref<WalletLedger[]>([]);

// 租后须知
const showPostRentalNotice = ref(false);
const loadingPostRentalNotice = ref(false);
const postRentalNotice = ref<PostRentalNotice | null>(null);

const settingsGroups = [
  {
    title: "账号与安全",
    items: [
      { label: "资料更改", icon: "edit", action: "profile" },
      { label: "实名认证", icon: "idcard", action: "realname" },
      { label: "注销账号", icon: "delete-o", action: "cancel-account" },
    ],
  },
  {
    title: "法律与隐私",
    items: [
      { label: "隐私政策", icon: "shield-o", action: "privacy" },
      { label: "用户协议", icon: "description", action: "terms" },
    ],
  },
];

onMounted(() => {
  if (!isLoggedIn.value) {
    router.replace({ path: "/m/login", query: { redirect: route.fullPath } });
  } else {
    loadBalance();
  }
});

async function loadBalance() {
  try {
    const wallet = await fetchWalletBalance();
    balance.value = wallet.available_balance;
  } catch {
    // 失败静默处理，显示为默认0
  }
}

async function openLedgers() {
  showLedgers.value = true;
  loadingLedgers.value = true;
  try {
    const res = await fetchWalletLedger(1, 20);
    ledgers.value = res.items;
  } catch {
    showToast({ message: "无法获取账单明细", icon: "cross" });
  } finally {
    loadingLedgers.value = false;
  }
}

async function openPostRentalNotice() {
  showPostRentalNotice.value = true;
  if (!postRentalNotice.value) {
    loadingPostRentalNotice.value = true;
    try {
      postRentalNotice.value = await fetchPostRentalNotice();
    } catch {
      showToast({ message: "无法获取租后须知", icon: "cross" });
    } finally {
      loadingPostRentalNotice.value = false;
    }
  }
}

function handleWithdraw() {
  showDialog({
    title: "提现提示",
    message: "为了您的资金安全，提现请前往电脑端网页版进行操作。",
  });
}

function goOrders(tabKey: string) {
  router.push({ path: "/m/orders", query: { tab: tabKey } });
}

function onSettingClick(action: string) {
  showSettings.value = false;
  switch (action) {
    case "profile":
      openProfileEditor();
      break;
    case "realname":
      router.push("/m/realname");
      break;
    case "cancel-account":
      showDialog({
        title: "注销账号",
        message: "注销后账号数据将无法恢复，确认注销吗？",
        showCancelButton: true,
        confirmButtonText: "确认注销",
        confirmButtonColor: "#ee0a24",
        cancelButtonText: "再想想",
      })
        .then(() => {
          showToast({ message: "注销功能开发中", icon: "info-o" });
        })
        .catch(() => {});
      break;
    case "privacy":
      showToast({ message: "隐私政策页面开发中", icon: "info-o" });
      break;
    case "terms":
      showToast({ message: "用户协议页面开发中", icon: "info-o" });
      break;
  }
}

function openProfileEditor() {
  profileForm.nickname = session.nickname || defaultName.value;
  profileForm.avatar_url = session.avatarUrl || "";
  showProfileEditor.value = true;
}

async function saveProfile() {
  const nickname = profileForm.nickname.trim();
  const avatarURL = profileForm.avatar_url.trim();
  if (!nickname) {
    showToast({ message: "请输入昵称", icon: "warning-o" });
    return;
  }
  if (nickname.length > 24) {
    showToast({ message: "昵称不能超过 24 个字符", icon: "warning-o" });
    return;
  }

  savingProfile.value = true;
  try {
    await session.updateProfile({ nickname, avatar_url: avatarURL });
    showProfileEditor.value = false;
    showToast({ message: "资料已更新", icon: "passed" });
    await loadBalance();
  } catch (error) {
    showToast({ message: readError(error, "资料更新失败"), icon: "cross" });
  } finally {
    savingProfile.value = false;
  }
}

function readError(error: unknown, fallback: string) {
  if (typeof error === "object" && error && "response" in error) {
    const response = (error as { response?: { data?: { message?: string } } }).response;
    return response?.data?.message || fallback;
  }
  return fallback;
}

function confirmLogout() {
  showSettings.value = false;
  showDialog({
    title: "退出登录",
    message: "确定要退出当前账号吗？",
    showCancelButton: true,
    confirmButtonText: "退出",
    confirmButtonColor: "#ee0a24",
    cancelButtonText: "取消",
  })
    .then(() => {
      session.logout();
      router.replace("/m");
    })
    .catch(() => {});
}

const isLoggedIn = computed(() => Boolean(session.token));
const isRealnameVerified = computed(() => session.realnameStatus === "verified");
const isRealnamePending = computed(() => session.realnameStatus === "pending");

const defaultName = computed(() =>
  session.phone
    ? `用户${session.phone.slice(-6)}`
    : `用户${session.userId || 883099}`
);
const displayName = computed(() => session.nickname || defaultName.value);
const displayId = computed(() => session.userId || 138865);
const maskedPhone = computed(() =>
  session.phone
    ? `${session.phone.slice(0, 3)}****${session.phone.slice(-4)}`
    : "登录后查看手机号"
);
const avatarText = computed(() => displayName.value.slice(0, 1));

function resolveAvatarURL(url: string | undefined | null) {
  if (!url) return "";
  if (url.includes("/api/files/object?key=avatar/")) {
    return url.replace("/api/files/object", "/api/public/files/object");
  }
  return url;
}
</script>

<template>
  <main class="mobile-profile-shell">
    <!-- 已登录区 -->
    <template v-if="isLoggedIn">
      <!-- Hero 顶部蓝色横幅 -->
      <section class="profile-hero">
        <div class="hero-top-row">
          <span class="app-title">个人中心</span>
          <button class="hero-setting-btn" @click="showSettings = true">
            <van-icon name="setting-o" :size="20" color="#374151" />
          </button>
        </div>
        <div class="profile-hero-content">
          <div class="avatar-wrap">
            <div class="avatar-circle">
              <img v-if="session.avatarUrl" :src="resolveAvatarURL(session.avatarUrl)" alt="" />
              <span v-else>{{ avatarText }}</span>
            </div>
          </div>
          <div class="hero-user">
            <div class="name-row">
              <h1 class="user-nickname">{{ displayName }}</h1>
              <van-tag v-if="isRealnameVerified" type="success" size="medium" round class="verified-tag">
                <van-icon name="shield" /> 已实名
              </van-tag>
              <van-tag v-else-if="isRealnamePending" type="warning" size="medium" round class="verified-tag">
                审核中
              </van-tag>
              <van-tag v-else type="danger" size="medium" round class="unverified-tag" @click="router.push('/m/realname')">
                未实名
              </van-tag>
            </div>
            <p class="user-meta">ID: {{ displayId }} · {{ maskedPhone }}</p>
          </div>
        </div>
      </section>

      <!-- 余额卡片 - 银行卡质感暗色卡片 -->
      <div class="balance-card">
        <div class="balance-row">
          <div class="balance-info">
            <span class="balance-label">账户可用余额(元)</span>
            <strong class="balance-value">¥{{ Math.round(Number(balance || 0)) }}</strong>
          </div>
          <button class="withdraw-btn" @click="handleWithdraw">提现</button>
        </div>
        <div class="balance-footer">
          <button class="detail-btn" @click="openLedgers">
            查看收支明细 <van-icon name="arrow" :size="10" />
          </button>
        </div>
      </div>

      <!-- 买家服务面板 -->
      <div class="menu-card">
        <div class="card-header">
          <h3>买家服务</h3>
          <span class="see-all" @click="goOrders('all')">
            全部订单 <van-icon name="arrow" :size="10" />
          </span>
        </div>
        <div class="grid-menu col-4">
          <div class="grid-item" @click="goOrders('pending_payment')">
            <div class="icon-wrap warning"><van-icon name="bill-o" :size="22" /></div>
            <span>待支付</span>
          </div>
          <div class="grid-item" @click="goOrders('pending_handoff')">
            <div class="icon-wrap info"><van-icon name="logistics" :size="22" /></div>
            <span>待交接</span>
          </div>
          <div class="grid-item" @click="goOrders('renting')">
            <div class="icon-wrap primary"><van-icon name="play-circle-o" :size="22" /></div>
            <span>使用中</span>
          </div>
          <div class="grid-item" @click="goOrders('completed')">
            <div class="icon-wrap success"><van-icon name="smile-o" :size="22" /></div>
            <span>已完成</span>
          </div>
        </div>
      </div>

      <!-- 卖家服务面板 -->
      <div class="menu-card">
        <div class="card-header">
          <h3>卖家服务</h3>
        </div>
        <div class="grid-menu col-3">
          <div class="grid-item" @click="router.push('/m/seller/listings/create')">
            <div class="icon-wrap orange"><van-icon name="plus" :size="22" /></div>
            <span>发布商品</span>
          </div>
          <div class="grid-item" @click="router.push('/m/seller/listings')">
            <div class="icon-wrap purple"><van-icon name="shop-o" :size="22" /></div>
            <span>我的商品</span>
          </div>
          <div class="grid-item" @click="openLedgers">
            <div class="icon-wrap teal"><van-icon name="balance-o" :size="22" /></div>
            <span>提现/账单</span>
          </div>
        </div>
      </div>

      <!-- 常用功能 Cell Group -->
      <div class="menu-card cell-card">
        <van-cell-group :border="false">
          <van-cell title="消息中心" icon="chat-o" is-link to="/m/messages" />
          <van-cell title="资料修改" icon="edit" is-link @click="openProfileEditor" />
          <van-cell title="实名认证" icon="idcard" is-link to="/m/realname">
            <template #value>
              <span :class="isRealnameVerified ? 'verified-color' : 'unverified-color'">
                {{ isRealnameVerified ? '已认证' : isRealnamePending ? '审核中' : '未认证' }}
              </span>
            </template>
          </van-cell>
          <van-cell title="租后须知" icon="info-o" is-link @click="openPostRentalNotice" />
          <van-cell title="系统设置" icon="setting-o" is-link @click="showSettings = true" />
        </van-cell-group>
      </div>
    </template>

    <!-- 账单明细 Popup -->
    <van-popup
      v-model:show="showLedgers"
      position="bottom"
      round
      class="ledgers-popup"
      :style="{ height: '65%' }"
    >
      <header class="popup-header">
        <h3>收支明细</h3>
        <button class="popup-close" @click="showLedgers = false">✕</button>
      </header>
      <div class="popup-body">
        <van-loading v-if="loadingLedgers" class="center-loading" vertical>加载中...</van-loading>
        <van-empty v-else-if="ledgers.length === 0" description="暂无收支记录" />
        <div v-else class="ledger-list">
          <div v-for="item in ledgers" :key="item.id" class="ledger-item">
            <div class="ledger-left">
              <strong class="ledger-remark">{{ item.remark || item.biz_type }}</strong>
              <span class="ledger-time">{{ formatDateMinute(item.created_at) }}</span>
            </div>
            <div class="ledger-right" :class="item.direction === 'in' ? 'in-color' : 'out-color'">
              {{ item.direction === 'in' ? '+' : '-' }}¥{{ Math.round(Number(item.amount || 0)) }}
            </div>
          </div>
        </div>
      </div>
    </van-popup>

    <!-- 租后须知 Popup -->
    <van-popup
      v-model:show="showPostRentalNotice"
      position="bottom"
      round
      class="notice-popup"
      :style="{ height: '75%' }"
    >
      <header class="popup-header">
        <h3>{{ postRentalNotice?.title || '租后须知' }}</h3>
        <button class="popup-close" @click="showPostRentalNotice = false">✕</button>
      </header>
      <div class="popup-body">
        <van-loading v-if="loadingPostRentalNotice" class="center-loading" vertical>加载中...</van-loading>
        <div v-else-if="postRentalNotice" class="notice-content">
          {{ postRentalNotice.content }}
        </div>
        <van-empty v-else description="暂无租后须知" />
      </div>
    </van-popup>

    <!-- 设置面板 - Popup -->
    <van-popup
      v-model:show="showSettings"
      position="right"
      :style="{ width: '80%', height: '100%' }"
    >
      <div class="settings-panel">
        <header class="settings-header">
          <h2>设置</h2>
          <button class="settings-close" @click="showSettings = false">
            <van-icon name="cross" :size="20" />
          </button>
        </header>

        <div
          v-for="group in settingsGroups"
          :key="group.title"
          class="settings-group"
        >
          <h3 class="settings-group-title">{{ group.title }}</h3>
          <van-cell-group :border="false">
            <van-cell
              v-for="item in group.items"
              :key="item.action"
              :title="item.label"
              :icon="item.icon"
              is-link
              @click="onSettingClick(item.action)"
            />
          </van-cell-group>
        </div>

        <div class="settings-logout">
          <van-button plain type="danger" block round @click="confirmLogout">
            退出登录
          </van-button>
        </div>
      </div>
    </van-popup>

    <!-- 个人资料编辑 Popup -->
    <van-popup
      v-model:show="showProfileEditor"
      position="bottom"
      round
      :style="{ maxWidth: '430px', margin: '0 auto', left: 0, right: 0 }"
    >
      <section class="profile-editor">
        <header class="profile-editor-header">
          <h2>资料更改</h2>
          <button type="button" @click="showProfileEditor = false">
            <van-icon name="cross" :size="20" />
          </button>
        </header>

        <div class="profile-preview">
          <div class="profile-preview-avatar clickable" @click="triggerAvatarUpload">
            <img v-if="profileForm.avatar_url" :src="resolveAvatarURL(profileForm.avatar_url)" alt="" />
            <span v-else>{{ (profileForm.nickname || defaultName).slice(0, 1) }}</span>
            <div class="avatar-upload-overlay">
              <van-icon name="photograph" :size="16" />
            </div>
            <van-loading v-if="uploadingAvatar" size="20px" class="avatar-loading" />
          </div>
          <div class="preview-info">
            <strong>{{ profileForm.nickname || defaultName }}</strong>
            <p>{{ maskedPhone }}</p>
            <van-button
              size="mini"
              type="primary"
              plain
              round
              icon="photograph"
              class="upload-btn"
              :loading="uploadingAvatar"
              @click="triggerAvatarUpload"
            >
              上传本地照片
            </van-button>
          </div>
          <!-- 隐藏的文件选择框 -->
          <input
            ref="avatarFileInput"
            type="file"
            accept="image/*"
            style="display: none"
            @change="handleAvatarFileChange"
          />
        </div>

        <label class="editor-field">
          <span>昵称</span>
          <input
            v-model="profileForm.nickname"
            maxlength="24"
            placeholder="请输入昵称"
          />
        </label>

        <label class="editor-field">
          <span>头像地址</span>
          <input
            v-model="profileForm.avatar_url"
            maxlength="512"
            placeholder="可填写图片 URL，留空使用文字头像"
          />
        </label>

        <van-button
          type="primary"
          block
          round
          class="profile-save-btn"
          :loading="savingProfile"
          loading-text="保存中..."
          @click="saveProfile"
        >
          保存资料
        </van-button>
      </section>
    </van-popup>

    <MobileBottomNav />
  </main>
</template>

<style scoped>
.mobile-profile-shell {
  min-height: 100dvh;
  background: #f6f8fa;
  color: #1a202c;
  padding: 0 0 calc(64px + env(safe-area-inset-bottom));
}

/* ========== Hero Header ========== */
.profile-hero {
  background: #ffffff;
  padding: 20px 16px 20px;
  border-bottom: 1px solid rgba(243, 244, 246, 0.8);
}

.hero-top-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.app-title {
  font-size: 16px;
  font-weight: 700;
  color: #111827;
  letter-spacing: 0.5px;
}

.hero-setting-btn {
  display: grid;
  width: 34px;
  height: 34px;
  place-items: center;
  border: none;
  background: #f3f4f6;
  border-radius: 999px;
  cursor: pointer;
  transition: background 0.2s;
}

.hero-setting-btn:active {
  background: #e5e7eb;
}

.profile-hero-content {
  display: flex;
  align-items: center;
  gap: 16px;
}

.avatar-circle {
  display: grid;
  width: 60px;
  height: 60px;
  place-items: center;
  border-radius: 999px;
  overflow: hidden;
  background: #eaf2ff;
  color: #1477ff;
  font-size: 24px;
  font-weight: 900;
  border: 1px solid #e5e7eb;
}

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

.hero-user {
  flex: 1;
  min-width: 0;
}

.name-row {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.user-nickname {
  margin: 0;
  font-size: 18px;
  font-weight: 800;
  color: #111827;
}

.verified-tag, .unverified-tag {
  font-size: 10px !important;
  height: 18px;
  padding: 0 6px;
  font-weight: 700;
  border: none;
}

.user-meta {
  margin: 4px 0 0;
  color: #6b7280;
  font-size: 12px;
}

/* ========== Balance Card ========== */
.balance-card {
  margin: 12px 16px 14px;
  padding: 16px;
  border-radius: 16px;
  background: #ffffff;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.02), 0 1px 4px rgba(0, 0, 0, 0.02);
  border: 1px solid rgba(243, 244, 246, 0.9);
}

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

.balance-info {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.balance-label {
  color: #9ca3af;
  font-size: 11px;
  font-weight: 600;
}

.balance-value {
  font-size: 26px;
  font-weight: 800;
  font-family: Inter, system-ui, sans-serif;
  color: #111827;
}

.withdraw-btn {
  background: transparent;
  border: 1px solid #1477ff;
  color: #1477ff;
  font-weight: 700;
  font-size: 12px;
  height: 28px;
  padding: 0 16px;
  border-radius: 999px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

.withdraw-btn:active {
  background: #1477ff;
  color: #ffffff;
}

.balance-footer {
  margin-top: 12px;
  padding-top: 10px;
  border-top: 1px solid #f3f4f6;
}

.detail-btn {
  background: transparent;
  border: none;
  color: #1477ff;
  font-weight: 700;
  font-size: 11px;
  display: flex;
  align-items: center;
  gap: 4px;
  cursor: pointer;
  padding: 0;
}

/* ========== Menu Cards ========== */
.menu-card {
  margin: 0 16px 14px;
  background: #ffffff;
  border-radius: 16px;
  padding: 16px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.02), 0 1px 4px rgba(0, 0, 0, 0.02);
  border: 1px solid rgba(243, 244, 246, 0.9);
}

.menu-card.cell-card {
  padding: 0;
  overflow: hidden;
}

.menu-card .card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 14px;
}

.menu-card .card-header h3 {
  margin: 0;
  font-size: 14px;
  font-weight: 800;
  color: #111827;
}

.see-all {
  font-size: 11px;
  color: #6b7280;
  display: flex;
  align-items: center;
  gap: 2px;
  cursor: pointer;
}

/* Grid Menu Layout */
.grid-menu {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.grid-menu.col-4 .grid-item {
  flex: 0 0 25%;
}

.grid-menu.col-3 .grid-item {
  flex: 0 0 33.33%;
}

.grid-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  cursor: pointer;
}

.grid-item:active .icon-wrap {
  transform: scale(0.92);
}

.icon-wrap {
  display: grid;
  width: 44px;
  height: 44px;
  place-items: center;
  border-radius: 12px;
  transition: transform 0.15s ease;
}

/* Vibrant pastel colors for menu icon wraps */
.icon-wrap.warning { color: #f59e0b; background: rgba(245, 158, 11, 0.08); }
.icon-wrap.info { color: #2563eb; background: rgba(37, 99, 235, 0.08); }
.icon-wrap.primary { color: #1477ff; background: rgba(20, 119, 255, 0.08); }
.icon-wrap.success { color: #10b981; background: rgba(16, 185, 129, 0.08); }
.icon-wrap.orange { color: #ff5f00; background: rgba(255, 95, 0, 0.08); }
.icon-wrap.purple { color: #8b5cf6; background: rgba(139, 92, 246, 0.08); }
.icon-wrap.teal { color: #0d9488; background: rgba(13, 148, 136, 0.08); }

.grid-item span {
  font-size: 11px;
  color: #374151;
  font-weight: 600;
}

/* Vant Cell Adjustments */
:deep(.van-cell-group) {
  background: transparent;
}
:deep(.van-cell) {
  padding: 14px 16px;
  font-size: 14px;
}
.verified-color {
  color: #10b981;
  font-weight: 600;
}
.unverified-color {
  color: #9ca3af;
}

/* 收支明细 Popup */
.ledgers-popup {
  display: flex;
  flex-direction: column;
}
.popup-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px;
  border-bottom: 1px solid #f3f4f6;
  flex-shrink: 0;
}
.popup-header h3 {
  margin: 0;
  font-size: 16px;
  font-weight: 800;
}
.popup-close {
  border: none;
  background: transparent;
  font-size: 16px;
  color: #9ca3af;
  cursor: pointer;
}
.popup-body {
  flex: 1;
  overflow-y: auto;
  padding: 0 16px;
}
.ledger-list {
  display: flex;
  flex-direction: column;
}
.ledger-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 0;
  border-bottom: 1px solid #f3f4f6;
}
.ledger-item:last-child {
  border-bottom: none;
}
.ledger-left {
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.ledger-remark {
  font-size: 14px;
  color: #1f2937;
  font-weight: 600;
}
.ledger-time {
  font-size: 11px;
  color: #9ca3af;
}
.ledger-right {
  font-size: 15px;
  font-weight: 800;
}
.in-color {
  color: #10b981;
}
.out-color {
  color: #ef4444;
}

/* 租后须知 Popup */
.notice-popup {
  display: flex;
  flex-direction: column;
}
.notice-content {
  padding: 16px 0;
  color: #374151;
  font-size: 14px;
  line-height: 1.8;
  white-space: pre-wrap;
  word-wrap: break-word;
}
.center-loading {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px 0;
}

/* ========== 设置面板 ========== */
.settings-panel {
  display: flex;
  flex-direction: column;
  height: 100%;
  background: #f5f6f8;
}

.settings-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px;
  background: #fff;
  border-bottom: 1px solid #eee;
}

.settings-header h2 {
  margin: 0;
  font-size: 17px;
  font-weight: 700;
}

.settings-close {
  display: grid;
  width: 32px;
  height: 32px;
  place-items: center;
  border: none;
  background: none;
  color: #999;
  cursor: pointer;
}

.settings-group {
  margin-top: 12px;
}

.settings-group-title {
  margin: 0 0 4px 16px;
  font-size: 13px;
  font-weight: 600;
  color: #999;
}

.settings-group :deep(.van-cell) {
  font-size: 15px;
}

.settings-logout {
  margin: auto 16px 24px;
  padding-top: 20px;
}

/* ========== 资料修改 ========== */
.profile-editor {
  padding: 16px 16px max(18px, env(safe-area-inset-bottom));
  background: #fff;
}

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

.profile-editor-header h2 {
  margin: 0;
  color: #17233d;
  font-size: 18px;
  font-weight: 800;
}

.profile-editor-header button {
  display: grid;
  width: 34px;
  height: 34px;
  place-items: center;
  border: none;
  border-radius: 12px;
  background: #f4f6f8;
  color: #64748b;
}

.profile-preview {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 18px 0;
  padding: 14px;
  border-radius: 14px;
  background: #f7faff;
}

.profile-preview-avatar {
  display: grid;
  width: 52px;
  height: 52px;
  flex: 0 0 auto;
  place-items: center;
  overflow: hidden;
  border-radius: 16px;
  background: #1477ff;
  color: #fff;
  font-size: 22px;
  font-weight: 900;
}

.profile-preview-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.profile-preview-avatar.clickable {
  cursor: pointer;
  position: relative;
}

.avatar-upload-overlay {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #ffffff;
  opacity: 0;
  transition: opacity 0.2s ease;
}

.profile-preview-avatar.clickable:hover .avatar-upload-overlay,
.profile-preview-avatar.clickable:active .avatar-upload-overlay {
  opacity: 1;
}

.avatar-loading {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.8);
}

.preview-info {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 4px;
}

.preview-info strong {
  display: block;
  max-width: 230px;
  overflow: hidden;
  color: #17233d;
  font-size: 16px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.preview-info p {
  margin: 0;
  color: #7b8798;
  font-size: 12px;
}

.upload-btn {
  margin-top: 4px;
  font-weight: 600;
}

.editor-field {
  display: block;
  margin-bottom: 12px;
}

.editor-field span {
  display: block;
  margin-bottom: 6px;
  color: #697386;
  font-size: 13px;
  font-weight: 700;
}

.editor-field input {
  box-sizing: border-box;
  width: 100%;
  height: 46px;
  border: 1px solid #e1e7ef;
  border-radius: 12px;
  background: #fff;
  color: #17233d;
  font-size: 14px;
  outline: none;
  padding: 0 12px;
}

.editor-field input:focus {
  border-color: #1477ff;
  box-shadow: 0 0 0 3px rgba(20, 119, 255, 0.1);
}

.profile-save-btn {
  height: 44px;
  margin-top: 6px;
  background: #1477ff;
  border-color: transparent;
  font-weight: 800;
}

/* ========== 响应式适配 ========== */
@media (min-width: 520px) {
  .mobile-profile-shell {
    max-width: 430px;
    margin: 0 auto;
    box-shadow: 0 0 0 1px rgba(23, 35, 61, 0.08);
  }
}
</style>
