/* ── Reset & Base ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { -webkit-text-size-adjust: 100%; height: -webkit-fill-available; }
body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: #f5f5f7; color: #1d1d1f; line-height: 1.5;
    overscroll-behavior: none;  /* 禁用微信内下拉露黑底 */
    -webkit-overflow-scrolling: touch;
    /* safe-area 变量，由 viewport-fit=cover 启用 */
    --sat: env(safe-area-inset-top, 0px);
    --sar: env(safe-area-inset-right, 0px);
    --sab: env(safe-area-inset-bottom, 0px);
    --sal: env(safe-area-inset-left, 0px);
}
a { color: #007aff; text-decoration: none; }

/* ── Container（手机宽，PC 上也像手机界面）── */
.container { max-width: 640px; margin: 0 auto; padding: 1rem 0.75rem;
             padding-left: calc(0.75rem + var(--sal, 0px));
             padding-right: calc(0.75rem + var(--sar, 0px)); }

/* ── Auth Pages ── */
.auth-page { display: flex; align-items: center; justify-content: center;
             min-height: -webkit-fill-available;
             min-height: calc(var(--vh, 1vh) * 100);
             background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); padding: 1rem;
             padding-top: calc(1rem + var(--sat, 0px));
             padding-bottom: calc(1rem + var(--sab, 0px)); }
.auth-card { background: white; padding: 2rem 1.5rem; border-radius: 16px; box-shadow: 0 20px 60px rgba(0,0,0,0.15);
             width: 380px; max-width: 100%; text-align: center; }
.auth-card h1 { font-size: 1.6rem; margin-bottom: 0.3rem; }
.auth-subtitle { color: #86868b; margin-bottom: 1.5rem; }
.auth-footer { margin-top: 1.2rem; color: #86868b; font-size: 0.85rem; }

/* ── Nav ── */
.nav { background: white; border-bottom: 1px solid #e5e5ea;
       padding: 0.6rem 0.75rem;
       padding-top: calc(0.6rem + var(--sat, 0px));
       padding-left: calc(0.75rem + var(--sal, 0px));
       padding-right: calc(0.75rem + var(--sar, 0px));
       display: flex; align-items: center; justify-content: space-between;
       position: sticky; top: 0; z-index: 100; max-width: 640px; margin: 0 auto; }
.nav-brand { font-weight: 700; font-size: 1.1rem; color: #1d1d1f !important; white-space: nowrap; }
.nav-toggle { display: none; background: none; border: none; font-size: 1.5rem;
              cursor: pointer; padding: 0.3rem; color: #515154; }
.nav-links { display: flex; gap: 1rem; align-items: center; }
.nav-links a { color: #515154; font-size: 0.85rem; padding: 0.3rem 0; }

/* ── Dashboard ── */
.dashboard-header { margin-bottom: 1rem; }
.dashboard-header h2 { font-size: 1.3rem; }
.quota-bar { margin-top: 0.6rem; }
.quota-bar span { font-size: 0.8rem; color: #86868b; }
.progress-bar { height: 5px; background: #e5e5ea; border-radius: 3px; margin-top: 3px; }
.progress-fill { height: 100%; background: #007aff; border-radius: 3px; }

/* ── 可折叠分区 ── */
.collapsible { background: white; border-radius: 10px; box-shadow: 0 1px 4px rgba(0,0,0,0.04);
               margin-bottom: 0.6rem; overflow: hidden; }
.section-header { display: flex; align-items: center; gap: 0.5rem; padding: 0.8rem 0.8rem;
                  cursor: pointer; user-select: none; -webkit-tap-highlight-color: transparent; }
.section-header:active { background: #f5f5f7; }
.section-header h3 { font-size: 0.95rem; flex: 1; }
.section-badge { font-size: 0.7rem; background: #f0f0f5; padding: 0.1rem 0.45rem;
                 border-radius: 10px; color: #86868b; min-width: 1.4rem; text-align: center; }
.section-toggle { font-size: 0.85rem; color: #aeaeb2; width: 1.2rem; text-align: center; }
.section-body { max-height: 0; overflow: hidden; transition: max-height 0.25s ease; }
.collapsible.open .section-body { max-height: none; }
.section-body > * { padding: 0 0.75rem 0.75rem; }

/* ── Character Grid ── */
.char-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0.5rem; }
.char-card-wrapper { position: relative; }
.char-card { background: #f9f9fb; border-radius: 10px; padding: 0.8rem 0.5rem; text-align: center;
             color: #1d1d1f !important; text-decoration: none !important; display: block; }
.char-card:active { background: #f0f0f5; }
.char-share-btn { position: absolute; top: 2px; right: 2px; width: 28px; height: 28px;
                  border: none; background: rgba(255,255,255,0.85); border-radius: 50%;
                  font-size: 0.8rem; cursor: pointer; display: flex; align-items: center;
                  justify-content: center; -webkit-tap-highlight-color: transparent;
                  box-shadow: 0 1px 3px rgba(0,0,0,0.1); }
.char-share-btn:active { background: #e5e5ea; }
.char-name { font-size: 0.95rem; font-weight: 600; margin-bottom: 0.2rem; }
.char-traits { font-size: 0.7rem; color: #86868b; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.char-source { font-size: 0.65rem; color: #aeaeb2; margin-top: 0.15rem;
               overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.gender-f { color: #ff2d55; }
.gender-m { color: #007aff; }
.gender-u { color: #aeaeb2; }

/* ── Novel List ── */
.novel-list { display: flex; flex-direction: column; gap: 0.4rem; }
.novel-item { display: flex; align-items: center; justify-content: space-between;
              background: #f9f9fb; padding: 0.7rem 0.8rem; border-radius: 8px; gap: 0.5rem; }
.novel-item span { font-size: 0.85rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }

/* ── Import ── */
.page-card { background: white; border-radius: 12px; padding: 1.2rem;
             box-shadow: 0 1px 4px rgba(0,0,0,0.04); }
.import-form { margin-top: 1.2rem; }
.file-drop { border: 2px dashed #c7c7cc; border-radius: 10px; padding: 1.5rem 1rem; text-align: center;
             cursor: pointer; -webkit-tap-highlight-color: transparent; }
.file-drop:active { border-color: #007aff; background: #f0f5ff; }
.file-icon { font-size: 2rem; margin-bottom: 0.4rem; }
.file-drop p { font-size: 0.85rem; }

/* ── Chat（模仿 IM 聊天工具）── */
.chat-container { display: flex; flex-direction: column;
                  height: calc(var(--vh, 1vh) * 100 - 44px - 2rem);
                  max-height: calc(var(--vh, 1vh) * 100 - 44px - 2rem);
                  /* 刘海屏底部安全区域 */
                  padding-bottom: var(--sab, 0px); }
.chat-header { display: flex; align-items: center; justify-content: space-between;
               padding-bottom: 0.6rem; border-bottom: 1px solid #e5e5ea; flex-shrink: 0; }
.chat-header h2 { font-size: 1rem; }
.chat-messages { flex: 1; overflow-y: auto; padding: 0.8rem 0; display: flex;
                 flex-direction: column; gap: 0.6rem; min-height: 0; }
.msg { max-width: 88%; padding: 0.6rem 0.85rem; line-height: 1.5; font-size: 0.9rem;
       word-break: break-word; position: relative; }
.msg-user { align-self: flex-end; background: #95ec69; color: #1d1d1f;
            border-radius: 14px 14px 4px 14px; }
.msg-character { align-self: flex-start; background: white; color: #1d1d1f;
                 border-radius: 14px 14px 14px 4px; box-shadow: 0 1px 3px rgba(0,0,0,0.06); }
.msg-label { font-size: 0.65rem; font-weight: 600; margin-bottom: 0.15rem; opacity: 0.5; }
.msg-content { white-space: pre-wrap; }
.typing-indicator { display: flex; align-items: center; gap: 4px; padding: 0.2rem 0; }
.typing-dot { width: 6px; height: 6px; background: #aeaeb2; border-radius: 50%;
              animation: typing-bounce 1.4s ease-in-out infinite; }
.typing-dot:nth-child(2) { animation-delay: 0.2s; }
.typing-dot:nth-child(3) { animation-delay: 0.4s; }
@keyframes typing-bounce { 0%,60%,100% { transform: translateY(0); } 30% { transform: translateY(-5px); } }

.chat-input-area { border-top: 1px solid #e5e5ea; padding-top: 0.6rem; flex-shrink: 0; }
.chat-form { display: flex; gap: 0.4rem; }
.chat-form input { flex: 1; padding: 0.6rem 0.9rem; border: 1px solid #c7c7cc; border-radius: 20px;
                   font-size: 16px; outline: none; transition: border-color 0.2s;
                   -webkit-appearance: none; }
.chat-form input:focus { border-color: #007aff; }
.chat-form button { padding: 0.6rem 1rem; background: #007aff; color: white;
                    border: none; border-radius: 20px; font-size: 0.95rem; cursor: pointer;
                    white-space: nowrap; -webkit-tap-highlight-color: transparent; }
.chat-form button:disabled { opacity: 0.4; cursor: not-allowed; }

/* ── Form Elements ── */
.form-group { margin-bottom: 0.8rem; }
.form-group input { width: 100%; padding: 0.7rem 0.9rem; border: 1px solid #c7c7cc;
                    border-radius: 8px; font-size: 16px; outline: none; -webkit-appearance: none; }
.form-group input:focus { border-color: #007aff; }
.btn { display: inline-block; padding: 0.5rem 1rem; border-radius: 8px;
       font-size: 0.85rem; cursor: pointer; border: 1px solid #c7c7cc;
       background: white; color: #1d1d1f; text-decoration: none !important;
       -webkit-tap-highlight-color: transparent; }
.btn:active { opacity: 0.7; }
.btn-primary { background: #007aff; color: white; border-color: #007aff; }
.btn-full { width: 100%; padding: 0.7rem; font-size: 0.95rem; border-radius: 10px; }
.btn-sm { padding: 0.35rem 0.7rem; font-size: 0.75rem; }

/* ── Utilities ── */
.alert { padding: 0.7rem 0.9rem; border-radius: 8px; margin-bottom: 0.8rem; font-size: 0.85rem; }
.alert-error { background: #ffe5e5; color: #d32f2f; }
.text-muted { color: #86868b; }
.quota-badge { font-size: 0.7rem; background: #f0f0f5; padding: 0.2rem 0.5rem; border-radius: 10px;
               white-space: nowrap; }

/* ── Subscribe / Plans ── */
.plan-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 0.6rem; margin: 1rem 0; }
.plan-card { background: white; border-radius: 10px; padding: 1rem; text-align: center;
             box-shadow: 0 1px 4px rgba(0,0,0,0.04); border: 1px solid #e5e5ea; }
.plan-name { font-size: 0.95rem; font-weight: 600; }
.plan-price { font-size: 1.5rem; font-weight: 700; color: #007aff; margin: 0.3rem 0; }
.plan-tokens { font-size: 0.8rem; color: #515154; }
.plan-desc { font-size: 0.7rem; color: #aeaeb2; margin: 0.2rem 0 0.6rem; }
.quota-status { margin-top: 0.8rem; }
.order-table { width: 100%; border-collapse: collapse; font-size: 0.75rem; }
.order-table th, .order-table td { padding: 0.35rem 0.4rem; text-align: left; border-bottom: 1px solid #e5e5ea; }
.order-table th { font-weight: 600; color: #86868b; }
.badge { padding: 0.15rem 0.4rem; border-radius: 5px; font-size: 0.7rem; background: #f0f0f5; }
.badge-success { background: #d4edda; color: #155724; }
.badge-warning { background: #fff3cd; color: #856404; }
.modal { display: flex; align-items: center; justify-content: center; position: fixed;
         top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.5); z-index: 1000; padding: 1rem; }
.modal-content { background: white; border-radius: 14px; padding: 1.5rem; width: 320px;
                 max-width: 100%; text-align: center; }
.qr-container { margin: 1.2rem 0; min-height: 140px; display: flex; align-items: center; justify-content: center; }
.qr-code-wrapper { background: white; padding: 0.4rem; border-radius: 6px; }
.qr-code-wrapper img { max-width: 100%; height: auto; }
.spinner { width: 32px; height: 32px; border: 3px solid #e5e5ea; border-top-color: #007aff;
           border-radius: 50%; animation: spin 0.8s linear infinite; margin: 0 auto 0.6rem; }
@keyframes spin { to { transform: rotate(360deg); } }
.alert-success { background: #d4edda; color: #155724; }

/* ── Setup Page ── */
.setup-option { display: block; padding: 0.8rem; border: 2px solid #e5e5ea; border-radius: 10px;
                cursor: pointer; -webkit-tap-highlight-color: transparent; }
.setup-option:active { border-color: #c7c7cc; }
.setup-option.selected { border-color: #007aff !important; background: #f0f5ff; }
.setup-option input[type="radio"] { margin-right: 0.4rem; }
.form-select { width: 100%; padding: 0.5rem 0.7rem; border: 1px solid #c7c7cc; border-radius: 8px;
               font-size: 0.9rem; background: white; }

/* ── 继续聊天 ── */
.conv-groups { display: flex; flex-direction: column; gap: 0.6rem; }
.conv-group { background: #f9f9fb; border-radius: 10px; overflow: hidden; }
.conv-group-header { display: flex; align-items: center; gap: 0.5rem; padding: 0.6rem 0.8rem 0.2rem;
                     font-size: 0.9rem; }
.conv-group-header .conv-source { font-size: 0.7rem; color: #aeaeb2; }
.conv-list { display: flex; flex-direction: column; }
.conv-item { display: flex; align-items: center; border-top: 1px solid #f0f0f5; }
.conv-item:active { background: #f0f0f5; }
.conv-item-main { flex: 1; display: flex; flex-direction: column; gap: 0.15rem;
                  padding: 0.55rem 0.5rem 0.55rem 0.8rem; color: #1d1d1f !important;
                  text-decoration: none !important; min-width: 0; }
.conv-item-top { display: flex; align-items: center; gap: 0.4rem; flex-wrap: wrap; }
.conv-mode-tag { font-size: 0.8rem; }
.conv-node { font-size: 0.7rem; color: #86868b; }
.conv-last-msg { font-size: 0.75rem; color: #aeaeb2; overflow: hidden; text-overflow: ellipsis;
                 white-space: nowrap; max-width: 100%; }
.conv-time { font-size: 0.7rem; color: #aeaeb2; }
.conv-delete-btn { flex-shrink: 0; width: 36px; height: 36px; border: none; background: none;
                   border-radius: 8px; cursor: pointer; font-size: 0.85rem; color: #c7c7cc;
                   margin-right: 0.3rem; display: flex; align-items: center; justify-content: center;
                   -webkit-tap-highlight-color: transparent; }
.conv-delete-btn:active { background: #ffe5e5; color: #ff3b30; }

/* ── 存档按钮 ── */
.chat-actions { display: flex; gap: 0.3rem; align-items: center; flex-wrap: wrap; }
.save-btn { font-size: 0.7rem; background: #f0f0f5; border: 1px solid #e5e5ea;
            padding: 0.25rem 0.5rem; border-radius: 10px; cursor: pointer; white-space: nowrap;
            -webkit-tap-highlight-color: transparent; }
.save-btn:active { background: #e5e5ea; }
.save-btn.saved { background: #d4edda; border-color: #c3e6cb; color: #155724; }

/* ── 超小屏适配 ── */
@media (max-width: 400px) {
    .char-grid { grid-template-columns: repeat(2, 1fr); }
    .msg { max-width: 92%; font-size: 0.85rem; }
    .chat-header h2 { font-size: 0.9rem; }
    .container { padding: 0.8rem 0.5rem; }
    .nav { padding: 0.5rem 0.5rem; }
}

/* ── 桌面宽屏（nav 居中跟随 container）── */
@media (min-width: 641px) {
    body { background: #e8e8ed; }
    .container { padding: 1rem; }
    .nav { padding: 0.6rem 1rem; }
}
