/* ===== Reset & Base ===== */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg-primary: #0f0f14;
  --bg-secondary: #1a1a24;
  --bg-card: #222230;
  --bg-input: #2a2a3a;
  --text-primary: #e8e8f0;
  --text-secondary: #9999aa;
  --text-muted: #666678;
  --accent: #7c6ff7;
  --accent-hover: #9080ff;
  --accent-dim: #5a4fd4;
  --danger: #f75c6c;
  --success: #4cdf80;
  --border: #333348;
  --radius: 12px;
  --radius-sm: 8px;
  --nav-height: 64px;
  --topbar-height: 52px;
  --max-width: 480px;
  --chat-bubble-user: #7c6ff7;
  --chat-bubble-ai: #222230;
}

/* ===== Loading Screen ===== */
#loading {
  position: fixed;
  inset: 0;
  z-index: 9999;
  background: var(--bg-primary);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  transition: opacity 0.3s ease;
}

#loading.hidden {
  opacity: 0;
  pointer-events: none;
}

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

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

.loading-text {
  color: var(--text-muted);
  font-size: 13px;
  letter-spacing: 0.5px;
}

html, body {
  height: 100%;
  width: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overflow: hidden;
}

/* ===== App Layout ===== */
#app {
  display: flex;
  flex-direction: column;
  height: 100%;
  max-width: var(--max-width);
  margin: 0 auto;
  position: relative;
  overflow: hidden;
}

.app-main {
  flex: 1;
  overflow: hidden;
  position: relative;
  min-height: 0;
}

.page {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.page.hidden {
  display: none !important;
}

/* 聊天页用 Flex 布局 */
#page-chat {
  background: var(--bg-primary);
}

/* 状态栏固定在顶部 */
.chat-topbar {
  position: fixed;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 100%;
  max-width: var(--max-width);
  height: var(--topbar-height);
  z-index: 50;
}

/* 消息区域：flex: 1 自动填充 */
#page-chat .chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 12px 8px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  min-height: 0;
}

/* ===== Conversation List ===== */
.conv-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  flex-shrink: 0;
}

.conv-header h2 {
  font-size: 20px;
  font-weight: 600;
}

.btn-add {
  font-size: 26px;
  color: var(--accent);
  font-weight: 300;
  line-height: 1;
}

.conv-list {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.conv-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  cursor: pointer;
  transition: background 0.15s;
  border-bottom: 0.5px solid var(--border);
}

.conv-item:active {
  background: var(--bg-input);
}

.conv-avatar {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: var(--bg-input);
  background-size: cover;
  background-position: center;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  flex-shrink: 0;
  overflow: hidden;
}

.conv-info {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.conv-name {
  font-size: 15px;
  font-weight: 600;
}

.conv-preview {
  font-size: 13px;
  color: var(--text-secondary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conv-meta {
  font-size: 11px;
  color: var(--text-muted);
  flex-shrink: 0;
  text-align: right;
  align-self: flex-start;
  margin-top: 4px;
}

.conv-badge {
  display: inline-block;
  background: var(--danger);
  color: #fff;
  font-size: 10px;
  border-radius: 10px;
  padding: 1px 7px;
  margin-top: 4px;
  min-width: 18px;
  text-align: center;
}

/* ===== Chat Top Bar ===== */
.chat-topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 8px 8px 4px;
  background: var(--bg-secondary);
  border-bottom: 0.5px solid var(--border);
  flex-shrink: 0;
  min-height: var(--topbar-height);
  position: relative;
  z-index: 50;
  width: 100%;
}

.btn-back {
  font-size: 28px;
  font-weight: 300;
  line-height: 1;
  padding: 4px 8px;
  min-width: auto;
}

.chat-topbar-info {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1;
  min-width: 0;
}

.chat-topbar-avatar {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: var(--bg-input);
  background-size: cover;
  background-position: center;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  flex-shrink: 0;
  overflow: hidden;
}

.chat-topbar-name {
  font-size: 16px;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.btn-more {
  font-size: 20px;
  font-weight: 700;
  letter-spacing: 2px;
}

/* Chat More Menu */
.chat-more-menu {
  position: fixed;
  top: calc(var(--topbar-height) + 2px);
  right: 8px;
  background: var(--bg-card);
  border: 0.5px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: 0 4px 16px rgba(0,0,0,0.4);
  z-index: 100;
  min-width: 140px;
  overflow: hidden;
}

.chat-more-menu.hidden {
  display: none;
}

.chat-more-item {
  display: block;
  width: 100%;
  padding: 12px 16px;
  background: none;
  border: none;
  color: var(--text-primary);
  font-size: 14px;
  text-align: left;
  cursor: pointer;
  transition: background 0.15s;
}

.chat-more-item:active {
  background: var(--bg-input);
}

.chat-more-danger {
  color: var(--danger);
  border-top: 0.5px solid var(--border);
}

/* ===== Chat Input ===== */
.btn-send {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  font-size: 18px;
  flex-shrink: 0;
  border: none;
  cursor: pointer;
  transition: all 0.2s;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.btn-send:disabled {
  opacity: 0.4;
  transform: none;
  cursor: not-allowed;
}

.send-icon {
  display: inline-block;
}

/* ===== Float Input Button ===== */
.btn-float-input {
  position: fixed;
  right: max(16px, calc((100vw - var(--max-width)) / 2 + 16px));
  bottom: max(120px, calc(env(safe-area-inset-bottom, 0px) + 120px));
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  font-size: 22px;
  border: none;
  cursor: pointer;
  box-shadow: 0 4px 16px rgba(124, 111, 247, 0.4);
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.2s, box-shadow 0.2s;
}

.btn-float-input:active {
  transform: scale(0.9);
  box-shadow: 0 2px 8px rgba(124, 111, 247, 0.25);
}

/* ===== Float Input Panel ===== */
.float-input-overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  animation: fadeIn 0.2s ease;
}

.float-input-overlay.hidden {
  display: none;
}

.float-input-panel {
  background: var(--bg-secondary);
  border-radius: var(--radius);
  padding: 16px;
  display: flex;
  gap: 10px;
  align-items: stretch;
  animation: slideUp 0.3s ease;
  border: 1px solid var(--border);
  margin: 0 16px;
  width: calc(100% - 32px);
  max-width: var(--max-width);
  transform: translateY(-50%);
}

.float-input-panel .btn-send {
  width: 40px;
  min-height: 44px;
  align-self: stretch;
  height: auto;
  border-radius: var(--radius-sm);
}

.float-input {
  flex: 1;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 14px;
  color: var(--text-primary);
  font-size: 15px;
  font-family: inherit;
  resize: none;
  outline: none;
  max-height: 120px;
  transition: border-color 0.2s;
}

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

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

/* ===== Chat Bubbles ===== */
.chat-bubble {
  max-width: 85%;
  padding: 10px 14px;
  border-radius: 18px;
  font-size: 14px;
  line-height: 1.6;
  word-break: break-word;
  animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

.chat-bubble.user {
  align-self: flex-end;
  background: var(--chat-bubble-user);
  color: #fff;
  border-bottom-right-radius: 6px;
  cursor: pointer;
  -webkit-tap-highlight-color: rgba(255,255,255,0.15);
}

.chat-bubble.ai {
  align-self: flex-start;
  background: var(--chat-bubble-ai);
  color: var(--text-primary);
  border-bottom-left-radius: 6px;
}

/* 气泡操作菜单 */
.bubble-menu {
  position: fixed;
  z-index: 2000;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: 0 4px 16px rgba(0,0,0,0.5);
  overflow: hidden;
  min-width: 120px;
  animation: fadeIn 0.15s ease;
}

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

.bubble-menu-item {
  display: block;
  width: 100%;
  padding: 10px 16px;
  background: none;
  border: none;
  color: var(--text-primary);
  font-size: 14px;
  text-align: left;
  cursor: pointer;
  transition: background 0.15s;
}

.bubble-menu-item:active {
  background: var(--bg-input);
}

.bubble-menu-item + .bubble-menu-item {
  border-top: 0.5px solid var(--border);
}

.chat-bubble.system {
  align-self: center;
  background: transparent;
  color: var(--text-muted);
  font-size: 12px;
  padding: 4px 12px;
  max-width: 100%;
}

.chat-bubble.typing {
  align-self: flex-start;
  background: var(--chat-bubble-ai);
  border-bottom-left-radius: 6px;
  display: flex;
  gap: 4px;
  padding: 14px 18px;
}

.typing-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-muted);
  animation: typingBounce 1.4s infinite ease-in-out;
}

.typing-dot:nth-child(2) { animation-delay: 0.2s; }
.typing-dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes typingBounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
  30% { transform: translateY(-6px); opacity: 1; }
}

/* ===== Empty State ===== */
.empty-state {
  text-align: center;
  padding: 48px 16px;
  color: var(--text-muted);
}

.empty-icon {
  font-size: 48px;
  margin-bottom: 12px;
}

.empty-avatar {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  background-size: cover;
  background-position: center;
  margin: 0 auto 12px;
}

.empty-state p {
  font-size: 14px;
}

.chat-empty {
  padding-top: 80px;
}

/* ===== Buttons ===== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: none;
  border-radius: var(--radius-sm);
  padding: 8px 16px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
  background: var(--bg-input);
  color: var(--text-primary);
  gap: 6px;
}

.btn:active {
  transform: scale(0.96);
}

.btn-primary {
  background: var(--accent);
  color: #fff;
}

.btn-primary:active {
  background: var(--accent-dim);
}

.btn-primary:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

.btn-secondary {
  background: var(--bg-card);
  color: var(--text-secondary);
}

.btn-sm {
  padding: 6px 12px;
  font-size: 13px;
  border-radius: 20px;
}

.btn-block {
  width: 100%;
  padding: 12px;
  font-size: 15px;
}

.btn-icon {
  background: none;
  padding: 6px;
  font-size: 18px;
  min-width: 34px;
  min-height: 34px;
}

.btn-danger {
  color: var(--danger);
}

/* ===== Settings ===== */
#page-settings {
  padding: 16px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.page-header h2 {
  font-size: 20px;
  font-weight: 600;
}

.settings-form {
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding-bottom: calc(var(--nav-height) + 16px);
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.form-group label {
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
}

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

.label-row label {
  margin: 0;
}

.form-group input[type="text"],
.form-group input[type="password"],
.form-group input[type="number"],
.form-group textarea {
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 14px;
  color: var(--text-primary);
  font-size: 14px;
  font-family: inherit;
  outline: none;
  transition: border-color 0.2s;
}

.form-group input:focus,
.form-group textarea:focus {
  border-color: var(--accent);
}

.form-group textarea {
  resize: vertical;
  min-height: 120px;
}

.form-hint {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 2px;
}

.form-row {
  display: flex;
  gap: 12px;
}

.form-group-half {
  flex: 1;
}

.form-section {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  margin-top: 8px;
}

.form-section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 14px;
  font-weight: 500;
  color: var(--text-primary);
}

/* iOS Toggle Switch */
.toggle-switch {
  position: relative;
  display: inline-block;
  width: 44px;
  height: 26px;
  flex-shrink: 0;
}

.toggle-switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle-slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--bg-input);
  border-radius: 13px;
  transition: 0.3s;
  border: 1px solid var(--border);
}

.toggle-slider::before {
  content: '';
  position: absolute;
  height: 20px;
  width: 20px;
  left: 2px;
  bottom: 2px;
  background: var(--text-muted);
  border-radius: 50%;
  transition: 0.3s;
}

.toggle-switch input:checked + .toggle-slider {
  background: var(--accent);
  border-color: var(--accent);
}

.toggle-switch input:checked + .toggle-slider::before {
  transform: translateX(18px);
  background: #fff;
}

/* 会话列表未读角标 */
.conv-badge {
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 18px;
  height: 18px;
  border-radius: 9px;
  background: #ef4444;
  color: #fff;
  font-size: 11px;
  font-weight: 600;
  padding: 0 5px;
  margin-top: 4px;
}

/* AI 消息基础样式（修复 assistant role 无样式问题） */
.chat-bubble.assistant {
  align-self: flex-start;
  background: var(--chat-bubble-ai);
  color: var(--text-primary);
  border-bottom-left-radius: 6px;
}

/* 角色主动发言：左侧彩色标记 */
.chat-bubble.assistant.proactive {
  border-left: 3px solid var(--accent);
}

.btn-sm {
  padding: 8px 16px;
  font-size: 13px;
  border-radius: var(--radius-sm);
}

.input-password-wrap {
  display: flex;
  gap: 4px;
}

.input-password-wrap input {
  flex: 1;
}

.range-wrap {
  display: flex;
  align-items: center;
  gap: 12px;
}

.range-wrap input[type="range"] {
  flex: 1;
  accent-color: var(--accent);
}

.range-value {
  font-size: 14px;
  color: var(--accent);
  min-width: 28px;
  text-align: right;
  font-weight: 600;
}

.settings-status {
  text-align: center;
  font-size: 13px;
  min-height: 20px;
}

.settings-status.success { color: var(--success); }
.settings-status.error { color: var(--danger); }

.settings-data-section {
  margin-top: 8px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
}

.settings-data-section h3 {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 4px;
}

.data-btns {
  display: flex;
  gap: 10px;
  margin-top: 12px;
}

.data-btns .btn {
  flex: 1;
}

/* ===== Avatar Upload ===== */
.avatar-upload {
  display: flex;
  align-items: center;
  gap: 12px;
}

.avatar-preview {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--bg-input);
  background-size: cover;
  background-position: center;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 28px;
  flex-shrink: 0;
  border: 2px dashed var(--border);
  overflow: hidden;
}

.avatar-preview.has-image {
  border-style: solid;
  border-color: var(--accent);
}

.sync-code-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.sync-code-val {
  flex: 1;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 8px 12px;
  font-size: 13px;
  color: var(--accent);
  font-family: monospace;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sync-code-row input {
  flex: 1;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 8px 12px;
  color: var(--text-primary);
  font-size: 13px;
  font-family: monospace;
  outline: none;
}

.sync-code-row input:focus {
  border-color: var(--accent);
}

/* ===== Modal ===== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 1000;
  animation: fadeIn 0.2s ease;
}

.modal-overlay.hidden {
  display: none;
}

.modal-content {
  background: var(--bg-secondary);
  border-radius: var(--radius) var(--radius) 0 0;
  width: 100%;
  max-width: var(--max-width);
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  animation: slideUp 0.3s ease;
}

@keyframes slideUp {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px;
  border-bottom: 1px solid var(--border);
}

.modal-header h3 {
  font-size: 17px;
  font-weight: 600;
}

.modal-body {
  padding: 16px;
  overflow-y: auto;
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.modal-footer {
  display: flex;
  gap: 10px;
  padding: 16px;
  border-top: 1px solid var(--border);
}

.modal-footer .btn {
  flex: 1;
  padding: 12px;
}

/* ===== Bottom Nav ===== */
.bottom-nav {
  display: flex;
  height: var(--nav-height);
  background: var(--bg-secondary);
  border-top: 0.5px solid var(--border);
  flex-shrink: 0;
  padding-bottom: env(safe-area-inset-bottom, 0);
}

.bottom-nav.hidden {
  display: none;
}

.nav-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 12px;
  cursor: pointer;
  transition: color 0.2s;
  padding: 0;
}

.nav-item.active {
  color: var(--accent);
}

.nav-icon {
  font-size: 22px;
}

.nav-label {
  font-size: 11px;
  font-weight: 500;
}

/* ===== Toast ===== */
.toast {
  position: fixed;
  bottom: calc(var(--nav-height) + 16px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-card);
  color: var(--text-primary);
  padding: 10px 20px;
  border-radius: 20px;
  font-size: 14px;
  z-index: 2000;
  box-shadow: 0 4px 16px rgba(0,0,0,0.3);
  animation: toastIn 0.3s ease;
  border: 1px solid var(--border);
}

.toast.hidden { display: none; }
.toast.success { border-color: var(--success); }
.toast.error { border-color: var(--danger); }

@keyframes toastIn {
  from { opacity: 0; transform: translateX(-50%) translateY(10px); }
  to { opacity: 1; transform: translateX(-50%) translateY(0); }
}

/* ===== Scrollbar ===== */
::-webkit-scrollbar {
  width: 4px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 2px;
}

/* ===== Image Cropper ===== */
.crop-overlay {
  position: fixed;
  inset: 0;
  z-index: 2000;
  background: rgba(0, 0, 0, 0.92);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 20px;
}

.crop-overlay.hidden {
  display: none;
}

.crop-container {
  position: relative;
  width: 280px;
  height: 280px;
}

.crop-container canvas {
  width: 100%;
  height: 100%;
  display: block;
  border-radius: 50%;
  touch-action: none;
}

.crop-frame {
  position: absolute;
  inset: -4px;
  border-radius: 50%;
  border: 3px solid rgba(255, 255, 255, 0.8);
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.5);
  pointer-events: none;
}

.crop-controls {
  width: 280px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.crop-zoom-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.crop-zoom-icon {
  font-size: 14px;
  flex-shrink: 0;
}

.crop-zoom-row input[type="range"] {
  flex: 1;
  accent-color: var(--accent);
}

.crop-btns {
  display: flex;
  gap: 10px;
}

.crop-btns .btn {
  flex: 1;
  padding: 12px;
}

/* ===== Responsive (Desktop) ===== */
@media (min-width: 481px) {
  #app {
    border-left: 1px solid var(--border);
    border-right: 1px solid var(--border);
  }

  body {
    background: #08080d;
  }

  .modal-content {
    border-radius: var(--radius);
    margin-bottom: 20px;
  }

  .chat-bubble {
    max-width: 70%;
  }
}
