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

:root {
  --primary: #534AB7;
  --primary-light: #EEEDFE;
  --primary-hover: #3C3489;
  --bg: #f5f5f7;
  --surface: #ffffff;
  --text: #1a1a1a;
  --text-secondary: #6e6e73;
  --text-tertiary: #aeaeb2;
  --border: #e5e5ea;
  --user-bubble: #534AB7;
  --user-text: #ffffff;
  --ai-bubble: #f0f0f5;
  --ai-text: #1a1a1a;
  --danger: #e24b4a;
  --success: #1D9E75;
  --radius: 12px;
  --radius-sm: 8px;
  --radius-lg: 16px;
  --shadow: 0 1px 3px rgba(0,0,0,0.08);
  --shadow-lg: 0 4px 20px rgba(0,0,0,0.12);
  --header-h: 52px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

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

#app { height: 100%; display: flex; flex-direction: column; }

/* Auth */
.auth-view {
  display: flex; align-items: center; justify-content: center;
  height: 100%; padding: 20px; background: linear-gradient(135deg, #EEEDFE 0%, #f5f5f7 100%);
}
.auth-card {
  background: var(--surface); border-radius: var(--radius-lg); padding: 32px 24px;
  width: 100%; max-width: 360px; box-shadow: var(--shadow-lg); text-align: center;
}
.auth-logo { margin-bottom: 12px; display: flex; justify-content: center; }
.auth-card h1 { font-size: 22px; font-weight: 600; color: var(--text); }
.auth-subtitle { font-size: 13px; color: var(--text-secondary); margin-top: 4px; margin-bottom: 24px; }
.auth-tabs { display: flex; gap: 0; margin-bottom: 20px; border-bottom: 1px solid var(--border); }
.auth-tab {
  flex: 1; padding: 10px; border: none; background: none; font-size: 15px;
  color: var(--text-secondary); cursor: pointer; position: relative; transition: color 0.2s;
}
.auth-tab.active { color: var(--primary); font-weight: 500; }
.auth-tab.active::after {
  content: ''; position: absolute; bottom: -1px; left: 0; right: 0; height: 2px;
  background: var(--primary); border-radius: 1px;
}
.auth-form { display: flex; flex-direction: column; gap: 12px; }
.auth-form input {
  padding: 12px 14px; border: 1px solid var(--border); border-radius: var(--radius-sm);
  font-size: 15px; outline: none; transition: border-color 0.2s; background: var(--bg);
}
.auth-form input:focus { border-color: var(--primary); background: var(--surface); }
.btn-primary {
  padding: 12px; background: var(--primary); color: #fff; border: none;
  border-radius: var(--radius-sm); font-size: 15px; font-weight: 500; cursor: pointer;
  transition: background 0.2s;
}
.btn-primary:hover { background: var(--primary-hover); }
.btn-primary:active { transform: scale(0.98); }
.btn-primary:disabled { opacity: 0.5; }
.btn-sm { padding: 8px 16px; font-size: 13px; }
.btn-secondary {
  padding: 8px 16px; background: var(--bg); color: var(--text); border: 1px solid var(--border);
  border-radius: var(--radius-sm); font-size: 13px; cursor: pointer; transition: background 0.2s;
}
.btn-secondary:hover { background: var(--border); }
.auth-error { color: var(--danger); font-size: 13px; min-height: 18px; margin-top: 8px; }
.auth-hint { font-size: 12px; color: var(--text-tertiary); margin-top: 12px; }

/* Chat layout */
.chat-view { display: flex; height: 100%; width: 100%; position: relative; }

.chat-header {
  height: var(--header-h); display: flex; align-items: center; gap: 8px;
  padding: 0 12px; background: var(--surface); border-bottom: 1px solid var(--border);
  position: relative; z-index: 10; flex-shrink: 0;
}
.chat-title { flex: 1; text-align: center; font-size: 16px; font-weight: 500; }
.icon-btn {
  width: 36px; height: 36px; border: none; background: none; cursor: pointer;
  display: flex; align-items: center; justify-content: center; border-radius: var(--radius-sm);
  color: var(--text); transition: background 0.2s;
}
.icon-btn:hover { background: var(--bg); }
.icon-btn-sm { width: 32px; height: 32px; border: none; background: none; cursor: pointer;
  display: flex; align-items: center; justify-content: center; border-radius: 6px; color: var(--text-secondary); }

.overlay {
  position: absolute; inset: 0; background: rgba(0,0,0,0.3); z-index: 19;
  opacity: 0; transition: opacity 0.25s;
}
.overlay.show { opacity: 1; }

.sidebar {
  position: absolute; left: 0; top: 0; bottom: 0; width: 280px; background: var(--surface);
  z-index: 20; transform: translateX(-100%); transition: transform 0.25s; display: flex;
  flex-direction: column; border-right: 1px solid var(--border);
}
.sidebar.open { transform: translateX(0); }
.sidebar-header { padding: 12px; border-bottom: 1px solid var(--border); }
.btn-new-chat {
  width: 100%; padding: 10px; border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--surface); color: var(--text); font-size: 14px; cursor: pointer;
  display: flex; align-items: center; justify-content: center; gap: 6px; transition: background 0.2s;
}
.btn-new-chat:hover { background: var(--primary-light); border-color: var(--primary); color: var(--primary); }
.session-list { flex: 1; overflow-y: auto; padding: 8px; }
.session-item {
  padding: 10px 12px; border-radius: var(--radius-sm); cursor: pointer; font-size: 14px;
  color: var(--text-secondary); margin-bottom: 2px; transition: background 0.15s;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: flex;
  align-items: center; gap: 8px;
}
.session-item:hover { background: var(--bg); }
.session-item.active { background: var(--primary-light); color: var(--primary); font-weight: 500; }
.session-item .del-session { margin-left: auto; opacity: 0; transition: opacity 0.2s; }
.session-item:hover .del-session { opacity: 1; }
.sidebar-footer { padding: 12px; border-top: 1px solid var(--border); }
.user-info { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
.user-avatar {
  width: 36px; height: 36px; border-radius: 50%; background: var(--primary); color: #fff;
  display: flex; align-items: center; justify-content: center; font-size: 16px; font-weight: 500; flex-shrink: 0;
}
.user-details { flex: 1; display: flex; flex-direction: column; }
.user-details span:first-child { font-size: 14px; font-weight: 500; }
.user-role { font-size: 11px; color: var(--text-tertiary); }
.btn-admin {
  width: 100%; padding: 8px; border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--surface); color: var(--text-secondary); font-size: 13px; cursor: pointer; transition: all 0.2s;
}
.btn-admin:hover { background: var(--primary-light); color: var(--primary); border-color: var(--primary); }

/* Messages */
.chat-main {
  flex: 1; display: flex; flex-direction: column; overflow: hidden; max-width: 800px;
  margin: 0 auto; width: 100%; position: relative;
}
.messages { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 16px 12px 8px; }

.welcome-screen { display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100%; padding: 20px; }
.welcome-icon { margin-bottom: 16px; }
.welcome-screen h2 { font-size: 20px; font-weight: 500; color: var(--text); margin-bottom: 24px; }
.suggestions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; width: 100%; max-width: 440px; }
.suggestion-card {
  padding: 14px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface);
  cursor: pointer; text-align: left; transition: all 0.2s; display: flex; flex-direction: column; gap: 4px;
}
.suggestion-card:hover { border-color: var(--primary); background: var(--primary-light); }
.suggestion-title { font-size: 14px; font-weight: 500; color: var(--text); }
.suggestion-desc { font-size: 12px; color: var(--text-secondary); }

.msg { margin-bottom: 16px; display: flex; gap: 10px; max-width: 100%; }
.msg.user { flex-direction: row-reverse; }
.msg-bubble {
  max-width: 78%; padding: 10px 14px; border-radius: var(--radius); font-size: 15px; line-height: 1.6;
  word-break: break-word; white-space: pre-wrap;
}
.msg.user .msg-bubble { background: var(--user-bubble); color: var(--user-text); border-bottom-right-radius: 4px; }
.msg.assistant .msg-bubble { background: var(--ai-bubble); color: var(--ai-text); border-bottom-left-radius: 4px; }
.msg-avatar {
  width: 30px; height: 30px; border-radius: 6px; flex-shrink: 0; display: flex; align-items: center;
  justify-content: center; font-size: 14px; font-weight: 500;
}
.msg.user .msg-avatar { background: var(--primary); color: #fff; }
.msg.assistant .msg-avatar { background: var(--primary-light); color: var(--primary); }
.msg-tool-badge {
  display: inline-block; padding: 2px 8px; background: var(--primary-light); color: var(--primary);
  border-radius: 10px; font-size: 11px; margin-top: 4px;
}

.typing-indicator { position: absolute; bottom: 80px; left: 20px; display: flex; gap: 4px; z-index: 5; }
.typing-indicator span {
  width: 8px; height: 8px; border-radius: 50%; background: var(--text-tertiary);
  animation: typing 1.4s infinite;
}
.typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.4s; }
@keyframes typing { 0%,60%,100% { opacity: 0.3; transform: scale(0.8); } 30% { opacity: 1; transform: scale(1.1); } }

/* Input */
.input-area { padding: 8px 12px calc(8px + var(--safe-bottom)); background: var(--surface); border-top: 1px solid var(--border); }
.input-row { display: flex; gap: 8px; align-items: flex-end; }
#message-input {
  flex: 1; padding: 10px 14px; border: 1px solid var(--border); border-radius: 20px; font-size: 15px;
  outline: none; resize: none; max-height: 120px; line-height: 1.5; background: var(--bg); transition: border-color 0.2s;
}
#message-input:focus { border-color: var(--primary); background: var(--surface); }
.send-btn {
  width: 40px; height: 40px; border: none; border-radius: 50%; background: var(--primary); color: #fff;
  cursor: pointer; display: flex; align-items: center; justify-content: center; transition: all 0.2s; flex-shrink: 0;
}
.send-btn:hover { background: var(--primary-hover); }
.send-btn:active { transform: scale(0.92); }
.send-btn:disabled { background: var(--text-tertiary); cursor: not-allowed; }
.input-info { display: flex; align-items: center; justify-content: space-between; padding: 4px 4px 0; }
.model-select {
  border: none; background: none; font-size: 12px; color: var(--text-secondary); cursor: pointer;
  outline: none; -webkit-appearance: none; padding-right: 4px;
}
.powered-by { font-size: 11px; color: var(--text-tertiary); }

/* Admin */
.admin-view { display: flex; flex-direction: column; height: 100%; overflow-y: auto; }
.admin-content { padding: 16px; max-width: 600px; margin: 0 auto; width: 100%; }
.admin-card { background: var(--surface); border-radius: var(--radius); padding: 16px; margin-bottom: 12px; box-shadow: var(--shadow); }
.admin-card h3 { font-size: 16px; font-weight: 500; margin-bottom: 12px; }
.admin-stats { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.stat-item { background: var(--bg); border-radius: var(--radius-sm); padding: 12px; text-align: center; }
.stat-value { font-size: 24px; font-weight: 600; color: var(--primary); }
.stat-label { font-size: 12px; color: var(--text-secondary); margin-top: 2px; }
.admin-url-row { display: flex; gap: 8px; }
.admin-input {
  flex: 1; padding: 8px 12px; border: 1px solid var(--border); border-radius: var(--radius-sm);
  font-size: 14px; outline: none;
}
.admin-input:focus { border-color: var(--primary); }
.admin-hint { font-size: 13px; color: var(--text-secondary); margin-bottom: 12px; }
.qr-container { display: flex; justify-content: center; padding: 16px; background: var(--bg); border-radius: var(--radius-sm); margin-bottom: 12px; }
.qr-placeholder { color: var(--text-tertiary); font-size: 14px; align-self: center; }
.qr-container img { max-width: 260px; border-radius: var(--radius-sm); }

/* Responsive */
@media (min-width: 768px) {
  .sidebar { position: relative; transform: translateX(0); }
  .chat-header { display: none; }
  .chat-main { padding-top: 0; }
  #menu-btn { display: none; }
  #overlay { display: none !important; }
  .messages { padding: 16px 20px; }
  .input-area { padding: 8px 20px; }
}
@media (min-width: 1024px) {
  .messages { padding: 20px 24px; }
}
