/* ============================================================
   Apple Design Language (iOS / macOS 风格)
   特性：深浅双主题跟随系统 · 毛玻璃 · 系统字体 · 安全区适配
   ============================================================ */

* { margin: 0; padding: 0; box-sizing: border-box; }

:root {
  color-scheme: light dark;
  --ios-blue: #007AFF;
  --ios-green: #34C759;
  --ios-red: #FF3B30;
  --ios-orange: #FF9500;
  --radius: 20px;
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Pro Display",
          "Helvetica Neue", "PingFang SC", "Hiragino Sans GB",
          "Microsoft YaHei", sans-serif;
  --mono: "SF Mono", ui-monospace, "Cascadia Code", Consolas, monospace;
}

/* ---------- 浅色模式 ---------- */
@media (prefers-color-scheme: light) {
  :root {
    --bg: #F2F2F7;                       /* iOS 系统分组背景 */
    --bg-grad: radial-gradient(1200px 600px at 20% -10%, #E5F1FF 0%, transparent 60%),
               radial-gradient(1000px 500px at 110% 10%, #EFE8FF 0%, transparent 55%);
    --bar-bg: rgba(248, 248, 252, 0.72); /* 半透明导航栏 */
    --field-bg: rgba(118, 118, 128, 0.12);
    --field-bg-solid: #E9E9EE;
    --bubble-ai: #FFFFFF;
    --bubble-user: #007AFF;
    --text: #1C1C1E;
    --text-2: #3C3C43;
    --text-3: #8E8E93;
    --border: rgba(60, 60, 67, 0.12);
    --shadow: 0 4px 24px rgba(0, 0, 0, 0.06);
    --shadow-msg: 0 1px 2px rgba(0, 0, 0, 0.05);
    --code-bg: #F5F5F7;
    --link: #0066CC;
  }
}

/* ---------- 深色模式 ---------- */
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --bg-grad: radial-gradient(1200px 600px at 20% -10%, #001A3F 0%, transparent 60%),
               radial-gradient(1000px 500px at 110% 10%, #1E1233 0%, transparent 55%);
    --bar-bg: rgba(22, 22, 26, 0.72);
    --field-bg: rgba(118, 118, 128, 0.24);
    --field-bg-solid: #2C2C2E;
    --bubble-ai: #1C1C1E;
    --bubble-user: #0A84FF;
    --text: #FFFFFF;
    --text-2: #EBEBF0;
    --text-3: #8E8E93;
    --border: rgba(84, 84, 88, 0.3);
    --shadow: 0 4px 24px rgba(0, 0, 0, 0.4);
    --shadow-msg: 0 1px 3px rgba(0, 0, 0, 0.4);
    --code-bg: #0C0C0E;
    --link: #409CFF;
  }
}

html { height: 100%; -webkit-text-size-adjust: 100%; }

body {
  font-family: var(--font);
  background: var(--bg);
  background-image: var(--bg-grad);
  background-attachment: fixed;
  color: var(--text);
  height: 100dvh;
  display: flex;
  justify-content: center;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  letter-spacing: -0.01em;
}

/* ============ 应用容器 ============ */
.app {
  width: 100%;
  max-width: 880px;
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

/* ============ 导航栏（毛玻璃） ============ */
.topbar {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(env(safe-area-inset-top, 0px) + 14px) 20px 12px;
  background: var(--bar-bg);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  backdrop-filter: blur(20px) saturate(180%);
  border-bottom: 0.5px solid var(--border);
  flex-shrink: 0;
  z-index: 10;
}

.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }

.logo {
  width: 30px; height: 30px;
  border-radius: 8px;
  background: linear-gradient(145deg, #0A84FF, #5E5CE6);
  display: flex; align-items: center; justify-content: center;
  font-size: 15px; color: #fff;
  box-shadow: 0 2px 6px rgba(10, 132, 255, 0.35);
  flex-shrink: 0;
}

.title { font-size: 17px; font-weight: 700; white-space: nowrap; }
.title-sub { font-size: 11px; color: var(--text-3); font-weight: 500; margin-top: 1px; }

.status { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }

.dot {
  width: 9px; height: 9px; border-radius: 50%;
  background: var(--ios-orange);
  display: inline-block;
  transition: background 0.3s;
}
.dot.online  { background: var(--ios-green);  box-shadow: 0 0 0 3px rgba(52, 199, 89, 0.18); }
.dot.offline { background: var(--ios-red);    box-shadow: 0 0 0 3px rgba(255, 59, 48, 0.18); }

#conn-text { font-size: 13px; color: var(--text-3); font-weight: 500; }

/* 模型选择（iOS 分段控件风格） */
.model-select {
  -webkit-appearance: none; appearance: none;
  background: var(--field-bg);
  color: var(--text);
  border: 0.5px solid var(--border);
  border-radius: 10px;
  padding: 7px 28px 7px 12px;
  font-size: 13px; font-weight: 500;
  font-family: var(--font);
  outline: none;
  max-width: 180px;
  cursor: pointer;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6' viewBox='0 0 10 6'%3E%3Cpath d='M1 1l4 4 4-4' stroke='%238E8E93' stroke-width='1.5' fill='none' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
}

/* 退出按钮 */
.logout-btn {
  width: 32px; height: 32px;
  border-radius: 10px;
  border: 0.5px solid var(--border);
  background: var(--field-bg);
  color: var(--text-3);
  font-size: 15px;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: color 0.2s, background 0.2s;
  flex-shrink: 0;
}
.logout-btn:hover { color: var(--ios-red); background: var(--field-bg); }

/* 新对话按钮：hover 用蓝色区分 */
#new-chat-btn:hover { color: var(--ios-blue); background: var(--field-bg); }
/* 模型管理按钮：hover 用蓝色区分 */
#model-manage-btn:hover { color: var(--ios-blue); background: var(--field-bg); }

/* ============ 模型管理弹窗 ============ */
.modal-overlay {
  position: fixed; inset: 0; z-index: 100;
  background: rgba(0, 0, 0, 0.45);
  display: none;
  padding: 20px;
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
}
.modal-overlay:not([hidden]) {
  display: flex; align-items: center; justify-content: center;
}
.modal {
  width: min(480px, 100%);
  max-height: min(75vh, 560px);
  display: flex; flex-direction: column;
  background: var(--bubble-ai);
  color: var(--text);
  border: 0.5px solid var(--border);
  border-radius: 18px;
  box-shadow: var(--shadow);
  overflow: hidden;
}
.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 0.5px solid var(--border);
  flex-shrink: 0;
}
.modal-header h3 { font-size: 16px; font-weight: 700; }
.modal-close {
  width: 28px; height: 28px; border-radius: 50%;
  border: none; cursor: pointer;
  background: var(--field-bg); color: var(--text-3);
  font-size: 12px; display: flex; align-items: center; justify-content: center;
}
.modal-close:hover { color: var(--text); }
.modal-body { padding: 14px 18px 18px; overflow-y: auto; flex: 1; }
.modal-tip {
  font-size: 12.5px; color: var(--text-3);
  line-height: 1.5; margin-bottom: 12px;
}
.model-list { display: flex; flex-direction: column; gap: 8px; }
.model-row {
  display: flex; align-items: center; justify-content: space-between;
  gap: 10px;
  padding: 10px 12px;
  border: 0.5px solid var(--border);
  border-radius: 12px;
}
.model-info { display: flex; align-items: center; gap: 8px; min-width: 0; flex: 1; }
.model-name {
  font-size: 13px; font-weight: 500;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.model-tag {
  font-size: 11px; font-weight: 600;
  padding: 2px 8px; border-radius: 99px; flex-shrink: 0;
}
.tag-loaded { background: rgba(52, 199, 89, 0.16); color: var(--ios-green); }
.tag-idle { background: var(--field-bg); color: var(--text-3); }
.model-action {
  flex-shrink: 0;
  border: none; cursor: pointer;
  padding: 6px 14px; border-radius: 10px;
  font-size: 13px; font-weight: 600;
  font-family: var(--font);
  transition: opacity 0.2s;
}
.model-action:disabled { opacity: 0.5; cursor: not-allowed; }
.act-load { background: var(--ios-blue); color: #fff; }
.act-unload { background: var(--field-bg); color: var(--text-2); }
.model-empty { color: var(--text-3); font-size: 13px; text-align: center; padding: 24px 0; }

/* ============ 聊天区 ============ */
.chat-area {
  flex: 1;
  overflow-y: auto;
  padding: 20px 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
}

.chat-area::-webkit-scrollbar { width: 8px; }
.chat-area::-webkit-scrollbar-thumb {
  background: var(--field-bg); border-radius: 4px;
}

/* 空状态 */
.empty-state {
  margin: auto;
  text-align: center;
  color: var(--text-3);
  user-select: none;
}
.empty-icon {
  width: 84px; height: 84px;
  margin: 0 auto 20px;
  border-radius: 50%;
  background: linear-gradient(145deg, #0A84FF22, #5E5CE622);
  border: 1px solid var(--border);
  display: flex; align-items: center; justify-content: center;
  font-size: 38px;
  animation: floaty 4s ease-in-out infinite;
}
@keyframes floaty {
  0%, 100% { transform: translateY(0); }
  50%      { transform: translateY(-8px); }
}
.empty-state p { font-size: 17px; font-weight: 600; color: var(--text-2); margin-bottom: 6px; }
.hint { font-size: 13px; color: var(--text-3); }

/* ============ 消息（iMessage 气泡风格） ============ */
.msg {
  display: flex;
  gap: 8px;
  max-width: 100%;
  animation: fadeIn 0.25s ease;
}
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(8px) scale(0.98); }
  to   { opacity: 1; transform: translateY(0) scale(1); }
}

/* 用户消息：右对齐蓝色气泡 */
.msg.user { flex-direction: row-reverse; }
.msg.user .bubble {
  background: var(--bubble-user);
  color: #fff;
  border-radius: 20px;
  border-bottom-right-radius: 6px;
  box-shadow: 0 2px 8px rgba(10, 132, 255, 0.25);
}

/* AI 消息：左对齐浅色气泡 */
.msg.assistant .bubble {
  background: var(--bubble-ai);
  color: var(--text);
  border: 0.5px solid var(--border);
  border-radius: 20px;
  border-bottom-left-radius: 6px;
  box-shadow: var(--shadow-msg);
}

.bubble {
  padding: 11px 16px;
  font-size: 15.5px;
  line-height: 1.6;
  word-break: break-word;
  white-space: pre-wrap;
  max-width: min(78%, 640px);
}
.msg.user .bubble { max-width: 82%; }

.bubble p { margin: 6px 0; }
.bubble pre {
  background: var(--code-bg);
  border: 0.5px solid var(--border);
  padding: 10px 14px;
  border-radius: 10px;
  overflow-x: auto;
  font-size: 13px;
  font-family: var(--mono);
  margin: 8px 0;
  white-space: pre-wrap;   /* 长行自动换行 */
  word-break: break-word;
}
.bubble code { font-family: var(--mono); }

/* 最终回答：保留换行并自动换行（防止一行长文字） */
.bubble .answer {
  white-space: pre-wrap;
  word-break: break-word;
  overflow-wrap: anywhere;
}

/* 思考过程（可折叠，默认折叠） */
.thinking-wrap {
  margin: 4px 0 8px;
  border-left: 2px solid var(--ios-blue);
  padding-left: 10px;
}
.thinking-wrap summary {
  list-style: none;
  cursor: pointer;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text-3);
  user-select: none;
  padding: 2px 0;
  display: flex;
  align-items: center;
  gap: 6px;
}
.thinking-wrap summary::-webkit-details-marker { display: none; }
.thinking-wrap summary::before {
  content: "";
  width: 6px; height: 6px;
  border-right: 1.5px solid currentColor;
  border-bottom: 1.5px solid currentColor;
  transform: rotate(-45deg);
  transition: transform 0.2s;
  flex-shrink: 0;
}
.thinking-wrap[open] summary::before { transform: rotate(45deg); }
.thinking-wrap[open] summary { color: var(--text-2); }

.thinking {
  color: var(--text-3);
  font-size: 13px;
  line-height: 1.6;
  margin: 6px 0 0;
  white-space: pre-wrap;
  word-break: break-word;
  opacity: 0.9;
}

/* 流式打字光标 */
.typing::after {
  content: "";
  display: inline-block;
  width: 7px; height: 15px;
  margin-left: 3px;
  background: var(--ios-blue);
  border-radius: 2px;
  animation: blink 1s steps(2) infinite;
  vertical-align: text-bottom;
}
@keyframes blink { 50% { opacity: 0; } }

/* 头像 */
.avatar {
  width: 30px; height: 30px;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 14px;
  margin-top: 4px;
}
.msg.user .avatar {
  background: linear-gradient(145deg, #0A84FF, #5E5CE6);
  box-shadow: 0 2px 6px rgba(10, 132, 255, 0.3);
}
.msg.assistant .avatar {
  background: var(--field-bg-solid);
  border: 0.5px solid var(--border);
}

/* ============ 输入栏（毛玻璃） ============ */
.input-bar {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  padding: 10px 14px calc(env(safe-area-inset-bottom, 0px) + 14px);
  background: var(--bar-bg);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  backdrop-filter: blur(20px) saturate(180%);
  border-top: 0.5px solid var(--border);
  flex-shrink: 0;
}

.input-bar textarea {
  flex: 1;
  -webkit-appearance: none; appearance: none;
  background: var(--field-bg);
  border: 0.5px solid var(--border);
  border-radius: 21px;
  color: var(--text);
  padding: 11px 18px;
  font-size: 16px;            /* iOS 上不小于 16px 防止聚焦缩放 */
  font-family: var(--font);
  resize: none;
  max-height: 140px;
  outline: none;
  transition: border-color 0.2s, background 0.2s;
  line-height: 1.4;
}
.input-bar textarea:focus {
  border-color: var(--ios-blue);
  background: var(--field-bg);
}
.input-bar textarea::placeholder { color: var(--text-3); }

.send-btn {
  width: 42px; height: 42px;
  border-radius: 50%;
  border: none;
  background: linear-gradient(145deg, #0A84FF, #0071E3);
  color: #fff;
  font-size: 17px;
  cursor: pointer;
  flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 3px 10px rgba(10, 132, 255, 0.35);
  transition: transform 0.15s, opacity 0.2s;
}
.send-btn:hover:not(:disabled) { transform: scale(1.06); }
.send-btn:active:not(:disabled) { transform: scale(0.94); }
.send-btn:disabled { opacity: 0.4; cursor: not-allowed; box-shadow: none; }

/* ============ 移动端适配（重点优化） ============ */
@media (max-width: 600px) {
  .app { max-width: 100%; }

  .topbar {
    padding: calc(env(safe-area-inset-top, 0px) + 10px) 14px 10px;
  }
  .title { font-size: 16px; }

  /* 移动端隐藏连接文字，只留状态点 */
  #conn-text { display: none; }

  /* 品牌区可收缩，文字超长省略号，避免与右侧状态区重叠 */
  .brand { min-width: 0; flex: 0 1 auto; overflow: hidden; }
  .title, .title-sub {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  /* 模型选择占满剩余空间 */
  .model-select {
    flex: 1;
    min-width: 0;
    max-width: none;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .status { flex: 1; justify-content: flex-end; min-width: 0; }
  .status .dot { flex-shrink: 0; }

  .chat-area { padding: 14px 10px; gap: 14px; }

  .bubble {
    font-size: 16px;
    max-width: 86%;
    padding: 12px 16px;
  }
  .msg.user .bubble { max-width: 88%; }

  /* 移动端隐藏头像，iMessage 更简洁 */
  .avatar { display: none; }
  .msg.user { gap: 0; }
  .msg.assistant { gap: 0; }

  .input-bar {
    padding: 8px 10px calc(env(safe-area-inset-bottom, 0px) + 12px);
  }
  .send-btn { width: 40px; height: 40px; }

  .empty-icon { width: 72px; height: 72px; font-size: 32px; }
}

/* iOS Safari 底部手势条适配 */
@supports (-webkit-touch-callout: none) {
  .chat-area { padding-bottom: 20px; }
}

/* 窄屏且横屏时收窄 */
@media (max-width: 600px) and (orientation: landscape) {
  .chat-area { padding-top: 8px; }
  .topbar { padding-top: calc(env(safe-area-inset-top, 0px) + 6px); }
}
