/* ============ 基础 ============ */
* {
  margin: 0; padding: 0; box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

:root {
  --black: #1a1a1a;
  --white: #ffffff;
  --gray: #888888;
  --gray-light: #b0b0b0;
  --line: #f0f0f0;
  --bg-soft: #f7f7f7;
  --bg-soft-hover: #f0f0f0;
  --bg-soft-active: #e8e8e8;
  --chat-bg: #fafafa;
  --danger: #e74c3c;
  --danger-soft: #fdf0ef;
  --success: #27ae60;
  --success-soft: #eafaf1;
  --ease-out: cubic-bezier(0.32, 0.72, 0, 1);
  --ease-pop: cubic-bezier(0.16, 1, 0.3, 1);
  --r-sm: 5px;
  --r-md: 7px;
  --r-lg: 9px;
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "PingFang SC", "Microsoft YaHei", sans-serif;
  font-size: 13px;
  background: #ffffff;
  color: var(--black);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

pre {
  margin: 0;
  padding: 0;
  font-family: inherit;
}

button {
  border: 0;
  background: transparent;
  font-family: inherit;
  cursor: pointer;
}

button,
.top-btn,
.term-btn,
.send-btn,
.pill-btn,
.form-back,
.toggle-btn,
.modal-btn,
.entry-btn,
.entry-text,
.btn-solid-full {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
  font-family: inherit;
}
.btn-solid-full,
.entry-btn {
  display: flex;
}

/* ============ 入口页 ============ */
.entry {
  position: fixed; inset: 0;
  display: flex; align-items: center; justify-content: center;
  background: var(--chat-bg);
  z-index: 100;
  padding: 0 20px;
  animation: fadeIn 0.3s ease;
}
.entry-card {
  width: 100%; max-width: 320px;
  background: var(--white);
  border-radius: 12px;
  padding: 50px 20px 32px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.06);
  text-align: center;
  position: relative;
}
.entry-card.shake { animation: shake 0.32s ease; }

.entry-back {
  position: absolute;
  top: 14px; left: 14px;
  font-size: 13px;
  color: #888;
  padding: 2px 0;
  transition: color 0.15s ease;
}
.entry-back:active { color: var(--black); }
.entry-title {
  font-size: 18px; font-weight: 600;
  letter-spacing: 2px;
  margin-bottom: 24px;
  color: var(--black);
}
#entryBody { transition: opacity 0.18s ease; }
#entryBody.fading { opacity: 0; }

.entry-hint {
  font-size: 12px;
  color: #555;
  line-height: 1.7;
  text-align: left;
  margin-bottom: 16px;
}

.entry-btn {
  width: 100%;
  height: 44px;
  padding: 0 18px;
  border-radius: 10px;
  font-size: 13px; font-weight: 500;
  margin-top: 8px;
  margin-bottom: 12px;
  box-sizing: border-box;
  transition: background 0.2s ease, transform 0.15s ease;
}
.entry-btn-primary { background: var(--black); color: var(--white); }
.entry-btn-primary:active { background: #333; transform: scale(0.98); }
.entry-btn-outline {
  background: var(--white); color: var(--black);
  border: 1px solid #e0e0e0;
}
.entry-btn-outline:active { background: var(--bg-soft); transform: scale(0.98); }

.entry-text {
  width: 100%;
  height: 30px;
  background: none;
  color: var(--gray);
  font-size: 12px;
  padding: 0;
  margin-top: 4px;
}
.entry-text:active { opacity: 0.6; }

.entry-field {
  width: 100%;
  height: 40px;
  padding: 0 12px;
  border: 1px solid #e0e0e0;
  border-radius: var(--r-md);
  font-size: 13px;
  font-family: inherit;
  background: var(--white);
  color: var(--black);
  outline: none;
  margin-bottom: 10px;
  transition: border-color 0.2s ease;
  -webkit-appearance: none;
  appearance: none;
}
.entry-field:focus { border-color: var(--black); }

.entry-error {
  font-size: 12px;
  color: var(--danger);
  text-align: left;
  margin: -6px 0 10px;
  min-height: 16px;
  line-height: 1.4;
}

/* 密码强度条 */
.pwd-strength {
  display: flex; gap: 4px;
  margin-bottom: 4px;
  height: 4px;
}
.pwd-strength .bar {
  flex: 1; height: 100%;
  border-radius: 2px;
  background: #e0e0e0;
  transition: background 0.2s ease;
}
.pwd-strength .bar.weak { background: var(--danger); }
.pwd-strength .bar.medium { background: #f39c12; }
.pwd-strength .bar.strong { background: var(--success); }
.pwd-strength-label {
  font-size: 11px; color: var(--gray);
  text-align: left;
  margin-bottom: 10px;
}

.entry-checkbox {
  display: flex; align-items: flex-start; gap: 6px;
  text-align: left;
  font-size: 11px; color: var(--gray);
  margin: 2px 0 12px;
  padding: 4px;
  border-radius: var(--r-sm);
  transition: outline 0.2s ease;
}
.entry-checkbox.flash { outline: 2px solid var(--black); }
.entry-checkbox input { margin-top: 2px; cursor: pointer; }

/* ============ 容器 ============ */
.app-container {
  width: 100%;
  max-width: 440px;
  height: 100dvh;
  margin: 0 auto;
  background: var(--white);
  position: relative;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ============ 头部 ============ */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  min-height: 44px;
  background: var(--white);
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
  z-index: 10;
}
.header-left {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  height: 28px;
  padding: 0 10px;
  border-radius: var(--r-sm);
  background: var(--bg-soft);
  cursor: pointer;
  transition: background 0.15s ease, transform 0.12s ease;
}
.header-left:active { background: var(--bg-soft-active); transform: scale(0.96); }
.header-left span:first-child {
  font-size: 12px; font-weight: 500;
  color: #333;
  line-height: 1;
  max-width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.header-actions {
  display: flex; gap: 6px;
  align-items: center;
}
.caret { color: var(--gray); font-size: 9px; line-height: 1; }

.top-btn {
  height: 28px;
  padding: 0 12px;
  background: var(--bg-soft);
  border-radius: var(--r-sm);
  font-size: 12px;
  color: #333;
  transition: background 0.15s ease, transform 0.12s ease;
}
.top-btn:active { background: var(--bg-soft-active); transform: scale(0.94); }
.top-btn.danger { color: var(--danger); }

/* ============ 对话区 ============ */
.chat-main {
  flex: 1;
  padding: 12px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  background: var(--chat-bg);
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
}

/* 欢迎/空状态 */
.chat-welcome {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 20px;
  color: var(--gray);
}
.chat-welcome .welcome-icon {
  font-size: 36px;
  margin-bottom: 12px;
  opacity: 0.4;
}
.chat-welcome .welcome-title {
  font-size: 15px;
  font-weight: 600;
  color: #555;
  margin-bottom: 6px;
}
.chat-welcome .welcome-sub {
  font-size: 12px;
  line-height: 1.6;
  max-width: 240px;
}

.message {
  max-width: 85%;
  padding: 8px 12px;
  border-radius: var(--r-md);
  font-size: 13px;
  line-height: 1.5;
  word-wrap: break-word;
  white-space: pre-wrap;
  animation: msgIn 0.28s var(--ease-out);
}
.message.user {
  align-self: flex-end;
  background: var(--black);
  color: var(--white);
  border-bottom-right-radius: 3px;
  user-select: none;
  -webkit-user-select: none;
}
.message.ai {
  align-self: flex-start;
  background: #f5f5f5;
  color: #444;
  border-bottom-left-radius: 3px;
  user-select: text;
  -webkit-user-select: text;
}
.message.ai pre {
  font-family: Menlo, Monaco, Consolas, monospace;
  font-size: 12px;
  white-space: pre-wrap;
  word-break: break-all;
}
.message.output {
  background: #f0f0f0;
  user-select: text;
  -webkit-user-select: text;
}
.message.output pre {
  font-size: 11px;
  white-space: pre;
  word-break: normal;
  overflow-x: auto;
  max-width: 100%;
}
.message.output pre.collapsed {
  max-height: 120px;
  overflow: hidden;
  mask-image: linear-gradient(to bottom, black 70%, transparent 100%);
  -webkit-mask-image: linear-gradient(to bottom, black 70%, transparent 100%);
}
.collapse-btn {
  display: block;
  margin-top: 6px;
  font-size: 11px;
  color: var(--gray);
  background: none;
  padding: 2px 0;
  cursor: pointer;
}
.collapse-btn:active { opacity: 0.6; }
.message.error {
  background: var(--danger-soft);
  color: #c0392b;
  border-left: 2px solid var(--danger);
}

/* 消息时间戳 */
.message-time {
  font-size: 10px;
  color: var(--gray-light);
  margin-top: 4px;
  line-height: 1;
  user-select: none;
}
.message.user .message-time { text-align: right; color: rgba(255,255,255,0.5); }

/* 加载中指示 */
.typing-indicator {
  align-self: flex-start;
  background: #f5f5f5;
  border-radius: var(--r-md);
  border-bottom-left-radius: 3px;
  padding: 12px 14px;
  display: flex;
  gap: 4px;
  animation: msgIn 0.28s var(--ease-out);
}
.typing-indicator span {
  width: 6px; height: 6px;
  border-radius: 50%;
  background: var(--gray-light);
  animation: typingBounce 1.4s infinite ease-in-out both;
}
.typing-indicator span:nth-child(1) { animation-delay: -0.32s; }
.typing-indicator span:nth-child(2) { animation-delay: -0.16s; }
@keyframes typingBounce {
  0%, 80%, 100% { transform: scale(0.6); opacity: 0.4; }
  40% { transform: scale(1); opacity: 1; }
}

/* ============ 底部输入 ============ */
.chat-footer {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 10px;
  padding-bottom: calc(8px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: var(--white);
  flex-shrink: 0;
}
.chat-input {
  flex: 1;
  min-width: 0;
  height: 34px;
  padding: 0 10px;
  border: 1px solid #e0e0e0;
  border-radius: var(--r-md);
  font-size: 13px;
  font-family: inherit;
  line-height: 34px;
  outline: none;
  background: var(--white);
  color: var(--black);
  transition: border-color 0.15s ease;
  -webkit-appearance: none;
  appearance: none;
}
.chat-input:focus { border-color: var(--gray-light); }
.chat-input::placeholder { color: var(--gray-light); }

.term-btn {
  height: 34px;
  padding: 0 12px;
  background: var(--white);
  color: var(--black);
  border: 1px solid #e0e0e0;
  border-radius: var(--r-md);
  font-size: 12px;
  flex-shrink: 0;
  transition: background 0.15s ease, transform 0.12s ease;
}
.term-btn:active { background: var(--bg-soft); transform: scale(0.95); }

.send-btn {
  height: 34px;
  padding: 0 14px;
  background: var(--black);
  color: var(--white);
  border-radius: var(--r-md);
  font-size: 12px;
  flex-shrink: 0;
  transition: background 0.15s ease, transform 0.12s ease;
}
.send-btn:active:not(:disabled) { background: #333; transform: scale(0.95); }
.send-btn:disabled { background: #ccc; cursor: not-allowed; }

/* ============ 遮罩 ============ */
.overlay {
  position: absolute; inset: 0;
  background: rgba(0,0,0,0.4);
  opacity: 0; visibility: hidden;
  transition: opacity 0.3s ease, visibility 0.3s ease;
  z-index: 20;
}
.overlay.show { opacity: 1; visibility: visible; }

/* ============ 底部抽屉 ============ */
.drawer {
  position: absolute;
  bottom: 0; left: 0; right: 0;
  background: var(--white);
  border-top-left-radius: var(--r-lg);
  border-top-right-radius: var(--r-lg);
  max-height: 85%;
  transform: translate3d(0, 100%, 0);
  transition: transform 0.32s var(--ease-out);
  z-index: 30;
  display: flex;
  flex-direction: column;
  box-shadow: 0 -4px 16px rgba(0,0,0,0.08);
  will-change: transform;
}
.drawer.show { transform: translate3d(0, 0, 0); }

.drawer-handle {
  width: 30px; height: 3px;
  background: #e0e0e0;
  border-radius: 2px;
  margin: 8px auto 0;
  flex-shrink: 0;
}
.drawer-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  min-height: 40px;
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
}
.drawer-head h1 {
  font-size: 13px;
  font-weight: 600;
  color: var(--black);
  line-height: 1;
  margin: 0;
}
.drawer-head .top-btn {
  height: 26px;
  padding: 0 10px;
}
.drawer-body {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
  -webkit-overflow-scrolling: touch;
}
.drawer-footer {
  padding: 10px 12px;
  padding-bottom: calc(10px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  flex-shrink: 0;
}

/* 服务器条目 */
.server-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px;
  border-radius: var(--r-md);
  margin-bottom: 6px;
  cursor: pointer;
  transition: background 0.15s ease, transform 0.12s ease;
  border: 1px solid transparent;
}
.server-item:active { background: var(--bg-soft); transform: scale(0.98); }
.server-item.active {
  background: var(--bg-soft);
  border-color: #dcdcdc;
}
.server-item::before {
  content: '';
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--gray-light);
  flex-shrink: 0;
}
.server-item.online::before { background: var(--success); }
.server-item.active::before { background: var(--black); }
.server-item.active.online::before { background: var(--success); box-shadow: 0 0 0 2px var(--success-soft); }

.server-meta { flex: 1; min-width: 0; }
.server-meta .name { font-size: 13px; font-weight: 500; color: #333; line-height: 1.3; }
.server-meta .sub {
  font-size: 11px; color: var(--gray);
  margin-top: 1px;
  line-height: 1.3;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.server-actions {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}

/* 文字胶囊按钮 */
.pill-btn {
  height: 24px;
  padding: 0 8px;
  border-radius: var(--r-sm);
  font-size: 11px;
  color: #666;
  background: #eaeaea;
  transition: background 0.15s ease, color 0.15s ease, transform 0.12s ease;
  white-space: nowrap;
}
.pill-btn:active { transform: scale(0.94); background: var(--bg-soft-active); color: var(--black); }
.pill-btn.danger { color: var(--danger); background: var(--danger-soft); }
.pill-btn.danger:active { background: var(--danger); color: var(--white); }

/* ============ 表单 ============ */
.form-group { margin-bottom: 10px; }
.form-group label {
  display: block;
  font-size: 12px;
  color: #4a4a4a;
  margin-bottom: 5px;
  line-height: 1.3;
}
.form-control {
  width: 100%;
  height: 38px;
  padding: 0 12px;
  border: 1px solid #dcdcdc;
  border-radius: var(--r-md);
  font-size: 13px;
  font-family: inherit;
  color: #333;
  outline: none;
  background: var(--white);
  transition: border-color 0.15s ease;
  -webkit-appearance: none;
  appearance: none;
}
.form-control:focus { border-color: var(--gray-light); }
.form-control::placeholder { color: var(--gray-light); }

.field-row { display: flex; gap: 6px; }
.field-row > * { flex: 1; }

.auth-toggle { display: flex; gap: 6px; margin-bottom: 10px; }
.auth-toggle .toggle-btn {
  flex: 1;
  height: 32px;
  border-radius: var(--r-sm);
  border: 1px solid #e0e0e0;
  background: var(--white);
  color: var(--black);
  font-size: 12px;
  transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease, transform 0.12s ease;
}
.auth-toggle .toggle-btn:active { transform: scale(0.97); }
.auth-toggle .toggle-btn.active {
  background: var(--black);
  color: var(--white);
  border-color: var(--black);
}

.form-back {
  height: 26px;
  padding: 0 10px;
  font-size: 12px;
  border: 0;
  background: var(--bg-soft);
  color: var(--gray);
  border-radius: var(--r-sm);
  margin-bottom: 10px;
}
.form-back:active {
  background: var(--bg-soft-active);
  color: var(--black);
}

.btn-solid-full {
  width: 100%;
  height: 40px;
  background: var(--black);
  color: var(--white);
  border-radius: var(--r-md);
  font-size: 13px;
  font-weight: 500;
  transition: background 0.15s ease, transform 0.12s ease;
}
.btn-solid-full:active { background: #333; transform: scale(0.98); }

/* ============ 终端 ============ */
.terminal {
  position: absolute;
  top: 0; right: 0; bottom: 0;
  width: 100%;
  background: var(--white);
  z-index: 40;
  display: flex;
  flex-direction: column;
  transform: translate3d(100%, 0, 0);
  transition: transform 0.3s var(--ease-out);
  will-change: transform;
}
.terminal.open { transform: translate3d(0, 0, 0); }

.terminal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  min-height: 40px;
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
}
.terminal-title { font-size: 13px; font-weight: 600; line-height: 1; }
.terminal-actions { display: flex; gap: 6px; }

.terminal-body {
  flex: 1;
  overflow-y: auto;
  overflow-x: auto;
  padding: 12px;
  font-family: Menlo, Monaco, Consolas, monospace;
  font-size: 12px;
  line-height: 1.7;
  white-space: pre;
  word-break: normal;
  background: #fafafa;
  -webkit-overflow-scrolling: touch;
  user-select: text;
  -webkit-user-select: text;
}
.terminal-body .t-line { animation: lineIn 0.2s ease; }
.terminal-body .t-warn {
  color: var(--black);
  font-weight: 600;
  border-left: 2px solid var(--black);
  padding-left: 6px;
  margin: 4px 0;
  white-space: pre-wrap;
}

.terminal-input {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 12px;
  padding-bottom: calc(8px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  font-family: Menlo, Monaco, Consolas, monospace;
  font-size: 12px;
  flex-shrink: 0;
  background: var(--white);
}
.terminal-input .prompt { color: var(--gray); }
.terminal-input input {
  flex: 1; min-width: 0;
  border: none; outline: none;
  font-family: inherit; font-size: inherit;
  background: transparent; color: var(--black);
  -webkit-appearance: none;
  appearance: none;
}

/* ============ 弹窗 ============ */
.modal-overlay {
  position: fixed; inset: 0;
  z-index: 999;
  background: rgba(0,0,0,0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  opacity: 0; visibility: hidden;
  transition: opacity 0.25s ease, visibility 0.25s ease;
  z-index: 50;
}
.modal-overlay.show { opacity: 1; visibility: visible; }

.modal {
  width: 100%;
  max-width: 280px;
  background: var(--white);
  border-radius: var(--r-lg);
  padding: 18px 16px 14px;
  transform: scale(0.95);
  transition: transform 0.28s var(--ease-pop);
  box-shadow: 0 8px 24px rgba(0,0,0,0.12);
}
.modal-overlay.show .modal { transform: scale(1); }

.modal-title {
  font-size: 14px;
  font-weight: 600;
  color: var(--black);
  margin-bottom: 8px;
  line-height: 1.3;
}
.modal-content {
  font-size: 13px;
  color: #555;
  line-height: 1.6;
  white-space: pre-line;
  word-break: break-all;
  margin-bottom: 14px;
}

.modal-actions { display: flex; gap: 6px; }
.modal-btn {
  flex: 1;
  height: 38px;
  border-radius: var(--r-md);
  font-size: 13px;
  font-weight: 500;
  transition: background 0.15s ease, transform 0.12s ease;
}
.modal-btn:active { transform: scale(0.97); }
.modal-btn.btn-secondary { background: var(--bg-soft); color: #555; }
.modal-btn.btn-secondary:active { background: var(--bg-soft-active); }
.modal-btn.btn-primary { background: var(--black); color: var(--white); }
.modal-btn.btn-primary:active { background: #333; }
.modal-btn.btn-danger { background: var(--danger); color: var(--white); }
.modal-btn.btn-danger:active { background: #c0392b; }

/* ============ 动画 ============ */
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes msgIn {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}
@keyframes lineIn {
  from { opacity: 0; transform: translateY(3px); }
  to { opacity: 1; transform: translateY(0); }
}
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-6px); }
  75% { transform: translateX(6px); }
}

/* ============ 极小屏 ============ */
@media (max-width: 380px) {
  .chat-footer { padding: 6px 8px; gap: 5px; padding-bottom: calc(6px + env(safe-area-inset-bottom)); }
  .chat-input { padding: 0 8px; font-size: 12px; }
  .term-btn, .send-btn { padding: 0 10px; font-size: 11px; }
  .header-left span:first-child { max-width: 110px; }
}
