/* ============================================
   LilPal 主样式 - 移动端优先
   ============================================ */

:root {
  --bg: #faf8fc;
  --bg-chat: #f0edf5;
  --surface: #ffffff;
  --text-primary: #1e1b4b;
  --text-secondary: #6b7280;
  --text-muted: #9ca3af;
  --bubble-user: #7c3aed;
  --bubble-user-text: #ffffff;
  --bubble-pal: #ffffff;
  --bubble-pal-text: #1e1b4b;
  --accent: #8b5cf6;
  --accent-light: #c4b5fd;
  --danger: #ef4444;
  --border: #e5e7eb;
  --radius-sm: 10px;
  --radius-md: 18px;
  --radius-lg: 24px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html, body {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;          /* JS 会在键盘弹起时动态改为 visualViewport.height */
  height: 100dvh;         /* 支持动态视口的浏览器直接用这个 */
  overflow: hidden;
  overscroll-behavior: none;  /* 禁止 iOS 橡皮筋效果导致的横向/纵向溢出滚动 */
  font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'PingFang SC', 'Helvetica Neue', sans-serif;
  background: var(--bg);
  color: var(--text-primary);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

input, button, textarea {
  font-family: inherit;
  font-size: inherit;
}

/* ============================================
   应用容器 / 视图切换
   ============================================ */

.app-container {
  height: 100%;
  width: 100%;
  max-width: 480px;
  margin: 0 auto;
  position: relative;
  overflow: hidden;
}

.app-view {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
}

.app-view.hidden {
  display: none;
}

/* Loading 视图 */
#view-loading {
  align-items: center;
  justify-content: center;
}

.loading-spinner {
  width: 36px;
  height: 36px;
  border: 3px solid var(--accent-light);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

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

/* ============================================
   登录页
   ============================================ */

.login-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 32px 28px;
  gap: 28px;
}

.login-header {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.login-logo {
  width: 90px;
  height: 90px;
  margin-bottom: 4px;
  transform: scale(0.8);
}

.login-title {
  font-size: 28px;
  font-weight: 700;
  color: var(--text-primary);
  letter-spacing: -0.5px;
}

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

.login-form {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.login-input {
  width: 100%;
  height: 50px;
  padding: 0 18px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-md);
  font-size: 16px;
  color: var(--text-primary);
  background: var(--surface);
  outline: none;
  transition: border-color 0.2s;
}

.login-input:focus {
  border-color: var(--accent);
}

.login-input::placeholder {
  color: var(--text-muted);
}

.sms-row {
  display: flex;
  gap: 10px;
}

.sms-row .login-input {
  flex: 1;
}

.btn-send-sms {
  flex-shrink: 0;
  height: 50px;
  padding: 0 16px;
  border: 1.5px solid var(--accent);
  border-radius: var(--radius-md);
  background: transparent;
  color: var(--accent);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.2s;
}

.btn-send-sms:active {
  background: var(--accent-light);
}

.btn-send-sms:disabled {
  border-color: var(--border);
  color: var(--text-muted);
  cursor: not-allowed;
}

.btn-send-sms.counting {
  border-color: var(--border);
  color: var(--text-muted);
}

.btn-login {
  width: 100%;
  height: 50px;
  border: none;
  border-radius: var(--radius-md);
  background: var(--accent);
  color: white;
  font-size: 17px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}

.btn-login:active {
  opacity: 0.85;
  transform: scale(0.98);
}

.btn-login:disabled {
  background: var(--accent-light);
  cursor: not-allowed;
  transform: none;
}

.login-error {
  color: var(--danger);
  font-size: 13px;
  text-align: center;
  min-height: 20px;
}

.login-error.hidden {
  visibility: hidden;
}

.login-footer {
  display: flex;
  gap: 16px;
  font-size: 12px;
}

.login-footer a {
  color: var(--text-secondary);
  text-decoration: none;
}

.login-footer a:active {
  color: var(--accent);
}

.login-icp {
  display: block;
  font-size: 12px;
  color: var(--text-secondary);
  text-decoration: none;
  text-align: center;
  padding-bottom: calc(12px + var(--safe-bottom));
}

.login-icp:active {
  color: var(--accent);
}

/* ============================================
   聊天页
   ============================================ */

.chat-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  height: 100%;
  background:
    radial-gradient(ellipse 80% 40% at 50% 0%, rgba(232, 225, 245, 0.8) 0%, transparent 60%),
    var(--bg-chat);
}

/* 顶部栏 */
.chat-topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  padding-top: calc(12px + env(safe-area-inset-top, 0px));
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.chat-topbar .pal-name {
  font-size: 17px;
  font-weight: 600;
  color: var(--text-primary);
}

.ai-badge {
  font-size: 9px;
  font-weight: 500;
  color: var(--text-muted);
  background: var(--bg-chat);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 1px 5px;
  margin-left: 6px;
  vertical-align: middle;
}

.btn-menu {
  width: 36px;
  height: 36px;
  border: none;
  border-radius: 50%;
  background: var(--surface);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.06);
  transition: background 0.15s;
}

.btn-menu:active {
  background: var(--bg-chat);
}

/* ============================================
   用户菜单
   ============================================ */

.user-menu {
  position: absolute;
  top: 54px;
  right: 12px;
  width: 220px;
  background: var(--surface);
  border-radius: var(--radius-sm);
  box-shadow: 0 4px 20px rgba(0,0,0,0.12);
  z-index: 100;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.user-menu.hidden {
  display: none;
}

.user-menu .menu-user-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.user-menu .menu-username {
  font-size: 16px;
  font-weight: 600;
  color: var(--text-primary);
}

.user-menu .menu-pal-info {
  display: none;
}

/* 用户名编辑 */
.menu-username-row {
  display: flex;
  align-items: center;
  gap: 6px;
}

.btn-edit-username {
  width: 24px;
  height: 24px;
  border: none;
  border-radius: 4px;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  padding: 0;
  transition: color 0.15s;
}

.btn-edit-username:active {
  color: var(--accent);
}

.btn-edit-username.hidden {
  display: none;
}

.edit-username-row {
  display: flex;
  gap: 6px;
  align-items: center;
}

.edit-username-row.hidden {
  display: none;
}

.edit-username-input {
  flex: 1;
  height: 32px;
  padding: 0 10px;
  border: 1.5px solid var(--accent);
  border-radius: 6px;
  font-size: 14px;
  color: var(--text-primary);
  background: var(--surface);
  outline: none;
  min-width: 0;
}

.btn-save-username,
.btn-cancel-username {
  height: 32px;
  padding: 0 10px;
  border: none;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  flex-shrink: 0;
  transition: opacity 0.15s;
}

.btn-save-username {
  background: var(--accent);
  color: white;
}

.btn-cancel-username {
  background: transparent;
  color: var(--text-secondary);
}

.btn-save-username:active,
.btn-cancel-username:active {
  opacity: 0.7;
}

.user-menu .menu-divider {
  height: 1px;
  background: var(--border);
}

.btn-menu-link {
  width: 100%;
  height: 40px;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--text-primary);
  font-size: 15px;
  cursor: pointer;
  text-align: center;
  transition: background 0.15s;
}

.btn-menu-link:active {
  background: var(--bg-chat);
}

.btn-logout {
  width: 100%;
  height: 40px;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--danger);
  font-size: 15px;
  cursor: pointer;
  text-align: center;
  transition: background 0.15s;
}

.btn-logout:active {
  background: #fef2f2;
}

/* ============================================
   角色区域 (上部 ~30%)
   ============================================ */

.character-area {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px 0 8px;
  position: relative;
  z-index: 0; /* 建立堆叠上下文，让 ::before 毛玻璃层夹在背景光晕和角色之间 */
  min-height: 160px;
  background:
    radial-gradient(ellipse 50% 50% at 50% 45%, rgba(167, 139, 250, 0.22) 0%, rgba(196, 181, 253, 0.10) 35%, transparent 65%),
    radial-gradient(ellipse 80% 100% at 50% 50%, rgba(240, 171, 252, 0.08) 0%, transparent 60%);
  transition: max-height 0.2s ease, padding 0.2s ease, min-height 0.2s ease;
  overflow: hidden;
}

/* 角色区域毛玻璃背景 — 下沿 20% 从上到下逐渐透明直至完全消失 */
.character-area::before {
  content: '';
  position: absolute;
  inset: 0;
  z-index: -1; /* 在角色之下、光晕背景之上 */
  background: rgba(240, 237, 245, 0.45);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  mask: linear-gradient(
    to bottom,
    rgba(0, 0, 0, 1) 0%,
    rgba(0, 0, 0, 1) 80%,
    transparent 100%
  );
  -webkit-mask: linear-gradient(
    to bottom,
    rgba(0, 0, 0, 1) 0%,
    rgba(0, 0, 0, 1) 80%,
    transparent 100%
  );
  pointer-events: none;
}

.character-area.keyboard-open {
  max-height: 0;
  min-height: 0;
  padding-top: 0;
  padding-bottom: 0;
}

/* ============================================
   消息列表 (下部 ~70%)
   ============================================ */

.messages-area {
  flex: 1;
  position: relative;
  overflow: hidden;
}

/* 消息区顶部渐变 — 固定不动，消息向上滚动时在此层下淡出 */
.messages-area::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 48px;
  background: linear-gradient(
    to bottom,
    rgba(240, 237, 245, 0.85) 0%,
    rgba(240, 237, 245, 0.5) 50%,
    rgba(240, 237, 245, 0.05) 100%
  );
  mask: linear-gradient(
    to bottom,
    rgba(0, 0, 0, 1) 0%,
    rgba(0, 0, 0, 1) 65%,
    transparent 100%
  );
  -webkit-mask: linear-gradient(
    to bottom,
    rgba(0, 0, 0, 1) 0%,
    rgba(0, 0, 0, 1) 65%,
    transparent 100%
  );
  z-index: 2;
  pointer-events: none;
}

.chat-messages {
  height: 100%;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  -webkit-overflow-scrolling: touch;
  scroll-behavior: smooth;
  overscroll-behavior: contain;  /* 防止 iOS 消息区滚动传播到视口，导致底部露出空白 */
}

/* 弹性占位块：消息少时把所有消息推向底部，消息多时自动缩为 0 */
.chat-messages::before {
  content: '';
  flex: 1 1 0%;
  min-height: 0;
}

/* 消息气泡 */
.message-wrapper {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  max-width: 85%;
}

.message-wrapper.user {
  align-self: flex-end;
  flex-direction: row-reverse;
}

.message-wrapper.pal {
  align-self: flex-start;
}

/* Pal 小头像 */
.pal-avatar-mini {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: radial-gradient(ellipse at 35% 30%, #f0abfc, #c4b5fd 60%);
  flex-shrink: 0;
  position: relative;
}

.pal-avatar-mini::after {
  content: '';
  position: absolute;
  width: 7px;
  height: 7px;
  background: #1e1b4b;
  border-radius: 50%;
  top: 8px;
  left: 6px;
}

.pal-avatar-mini::before {
  content: '';
  position: absolute;
  width: 7px;
  height: 7px;
  background: #1e1b4b;
  border-radius: 50%;
  top: 8px;
  right: 6px;
}

/* 气泡 */
.bubble {
  padding: 10px 14px;
  border-radius: 18px;
  font-size: 15px;
  line-height: 1.5;
  word-break: break-word;
  max-width: min(60vw, 100%);
  min-width: 0; /* 允许 flex 收缩，长 URL 等不可断行内容不会撑破容器 */
  position: relative;
}

.bubble.user {
  background: var(--bubble-user);
  color: var(--bubble-user-text);
  border-bottom-right-radius: 6px;
}

.bubble.pal {
  background: var(--bubble-pal);
  color: var(--bubble-pal-text);
  border-bottom-left-radius: 6px;
  box-shadow: 0 1px 2px rgba(0,0,0,0.04);
}

/* 缓冲加载动画 - 三点跳动 */
.loading-dots {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px 2px;
}

.loading-dots span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--accent-light);
  animation: dot-bounce 1.4s ease-in-out infinite both;
}

.loading-dots span:nth-child(1) { animation-delay: 0s; }
.loading-dots span:nth-child(2) { animation-delay: 0.16s; }
.loading-dots span:nth-child(3) { animation-delay: 0.32s; }

@keyframes dot-bounce {
  0%, 80%, 100% { transform: scale(0.5); opacity: 0.3; }
  40% { transform: scale(1); opacity: 1; }
}

/* ============================================
   输入区域
   ============================================ */

.chat-input-area {
  flex-shrink: 0;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 14px;
  padding-bottom: calc(10px + var(--safe-bottom));
  background: var(--bg);
  border-top: 1px solid var(--border);
  position: relative;
  z-index: 10;
  transition: transform 0.15s ease-out;
}

.chat-input {
  flex: 1;
  min-height: 40px;
  max-height: 100px;
  padding: 9px 16px;
  border: 1.5px solid var(--border);
  border-radius: 20px;
  font-size: 16px; /* 必须 >= 16px，否则 iOS Safari 聚焦时自动放大页面，导致可左右滑动 */
  color: var(--text-primary);
  background: var(--surface);
  outline: none;
  resize: none;
  line-height: 1.4;
  transition: border-color 0.2s;
}

.chat-input:focus {
  border-color: var(--accent);
}

.chat-input::placeholder {
  color: var(--text-muted);
}

.chat-input:disabled {
  background: var(--bg-chat);
}

/* 发送按钮 */
.btn-send {
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 50%;
  background: var(--border);
  color: #fff;
  cursor: pointer;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s, transform 0.15s, box-shadow 0.2s;
  -webkit-tap-highlight-color: transparent;
}

.btn-send svg {
  opacity: 0.5;
  transition: opacity 0.2s;
}

.btn-send.has-text {
  background: var(--accent);
  box-shadow: 0 2px 8px rgba(139, 92, 246, 0.35);
}

.btn-send.has-text svg {
  opacity: 1;
}

.btn-send:hover:not(:disabled) {
  background: var(--accent);
}

.btn-send:hover:not(:disabled) svg {
  opacity: 1;
}

.btn-send:active {
  transform: scale(0.92);
}

.btn-send:disabled {
  background: var(--border);
  color: #fff;
  box-shadow: none;
  transform: none;
}

.btn-send:disabled svg {
  opacity: 0.3;
}

/* ========== PWA 安装横幅 ========== */
.pwa-install-banner {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  background: #fff3e0;
  border-bottom: 1px solid #e0c8a8;
  font-size: 13px;
  color: #5d4037;
}

.pwa-install-banner #btn-install-pwa {
  flex-shrink: 0;
  padding: 4px 12px;
  background: #8b5e3c;
  color: #fff;
  border: none;
  border-radius: 4px;
  font-size: 13px;
  cursor: pointer;
}

.pwa-install-banner .btn-dismiss {
  flex-shrink: 0;
  padding: 4px 8px;
  background: none;
  border: none;
  color: #999;
  font-size: 16px;
  cursor: pointer;
}

/* ========== iOS / Android 安装引导覆盖层 ========== */
.ios-install-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
}

.ios-install-box {
  background: #fff;
  border-radius: 12px;
  padding: 24px 20px;
  text-align: center;
  max-width: 280px;
}

.ios-install-box .ios-install-icon { font-size: 40px; margin-bottom: 12px; }
.ios-install-box p { margin: 4px 0; font-size: 15px; color: #333; }
.ios-install-box .ios-install-arrow { font-size: 24px; margin: 8px 0; color: #8b5e3c; }
.ios-install-box .btn-close-guide {
  margin-top: 12px;
  padding: 8px 24px;
  background: #8b5e3c;
  color: #fff;
  border: none;
  border-radius: 6px;
  font-size: 14px;
  cursor: pointer;
}

/* ---------- 通知订阅引导弹窗 ---------- */
.notify-prompt-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
}

.notify-prompt-box {
  background: #fff;
  border-radius: 12px;
  padding: 24px 20px;
  text-align: center;
  max-width: 280px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.15);
}

.notify-prompt-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
  margin-bottom: 8px;
}

.notify-prompt-body {
  font-size: 14px;
  color: #666;
  line-height: 1.5;
  margin-bottom: 20px;
}

.btn-notify-enable {
  display: block;
  width: 100%;
  padding: 10px 0;
  background: #8b5e3c;
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: 15px;
  cursor: pointer;
  margin-bottom: 8px;
}

.btn-notify-later {
  display: block;
  width: 100%;
  padding: 8px 0;
  background: transparent;
  color: #999;
  border: none;
  font-size: 13px;
  cursor: pointer;
}
