:root {
  --bg-main: #212121;
  --bg-sidebar: #171717;
  --bg-input: #303030;
  --bg-bubble: #303030;
  --bg-hover: #2f2f2f;
  --bg-toolbar-btn: #303030;
  --text-primary: #e8e8e8;
  --text-secondary: #b4b4b4;
  --text-tertiary: #8e8e8e;
  --text-hint: #666;
  --border-subtle: rgba(255,255,255,0.08);
  --border-input: rgba(255,255,255,0.12);
  --accent-purple: #8b5cf6;
  --accent-purple-dark: #6d28d9;
  --radius-sm: 8px;
  --radius-md: 16px;
  --radius-lg: 24px;
  --radius-full: 9999px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --sidebar-width: 300px;
}

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

html, body {
  height: 100%;
  overflow: hidden;
  touch-action: manipulation;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'Helvetica Neue',
               'PingFang SC', 'Noto Sans SC', 'Microsoft YaHei', sans-serif;
  background: var(--bg-main);
  color: var(--text-primary);
  font-size: 16px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

button { font-family: inherit; }

/* ========== App Shell ========== */
.app {
  display: flex;
  height: 100%;
  position: relative;
  overflow: hidden;
}

/* ========== Sidebar Overlay ========== */
.sidebar-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  z-index: 90;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;
}
.sidebar-overlay.active {
  opacity: 1;
  pointer-events: auto;
}

/* ========== Sidebar ========== */
.sidebar {
  position: fixed;
  top: 0; left: 0; bottom: 0;
  width: var(--sidebar-width);
  background: var(--bg-sidebar);
  z-index: 100;
  display: flex;
  flex-direction: column;
  transform: translateX(-100%);
  transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  padding-top: var(--safe-top);
}
.sidebar.open { transform: translateX(0); }

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  height: 52px;
  flex-shrink: 0;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 16px;
  font-weight: 600;
}

.sidebar-brand .brand-icon {
  width: 28px; height: 28px;
  background: #fff;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.sidebar-brand .brand-icon svg { width: 18px; height: 18px; }

.sidebar-new-chat {
  width: 36px; height: 36px;
  border: none;
  background: none;
  color: var(--text-secondary);
  display: flex; align-items: center; justify-content: center;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 0.15s;
}
.sidebar-new-chat:hover { background: var(--bg-hover); }
.sidebar-new-chat svg { width: 20px; height: 20px; }

.sidebar-body {
  flex: 1;
  overflow-y: auto;
  padding: 4px 8px;
}

.sidebar-section-label {
  padding: 12px 12px 6px;
  font-size: 13px;
  color: var(--text-tertiary);
  font-weight: 500;
}

.chat-item {
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  color: var(--text-secondary);
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: background 0.15s;
}
.chat-item:hover { background: var(--bg-hover); }
.chat-item.active { background: var(--bg-hover); color: var(--text-primary); }

.sidebar-footer {
  padding: 12px 16px;
  border-top: 1px solid var(--border-subtle);
  flex-shrink: 0;
}

.hub-link {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 4px;
  margin-bottom: 8px;
  font-size: 13px;
  color: var(--text-tertiary);
  text-decoration: none;
  border-radius: var(--radius-sm);
  transition: background 0.15s, color 0.15s;
}
.hub-link:hover { background: var(--bg-hover); color: var(--text-secondary); }
.hub-link svg { width: 18px; height: 18px; flex-shrink: 0; }

.user-info {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 4px;
}

.user-avatar {
  width: 32px; height: 32px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--accent-purple), #06b6d4);
  display: flex; align-items: center; justify-content: center;
  font-size: 14px; font-weight: 600; color: #fff;
  flex-shrink: 0;
}

.user-name { font-size: 14px; }

/* ========== Main ========== */
.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  width: 100%;
}

/* ========== Top Bar ========== */
.topbar {
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 8px;
  flex-shrink: 0;
  padding-top: var(--safe-top);
  position: relative;
  z-index: 10;
}

.icon-btn {
  width: 40px; height: 40px;
  display: flex; align-items: center; justify-content: center;
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  border-radius: var(--radius-sm);
  transition: background 0.15s;
  flex-shrink: 0;
  position: relative;
}
.icon-btn:hover { background: var(--bg-hover); }
.icon-btn svg { width: 22px; height: 22px; }

.menu-dot {
  position: absolute;
  top: 7px; right: 5px;
  width: 7px; height: 7px;
  border-radius: 50%;
  background: #3b82f6;
}

.model-selector {
  display: flex;
  align-items: center;
  gap: 4px;
  background: none;
  border: none;
  color: var(--text-primary);
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  padding: 6px 10px;
  border-radius: var(--radius-sm);
  transition: background 0.15s;
}
.model-selector:hover { background: var(--bg-hover); }
.model-selector svg {
  width: 14px; height: 14px;
  opacity: 0.5;
  flex-shrink: 0;
}

/* ========== Chat Area ========== */
.chat-area {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  display: flex;
  flex-direction: column;
}

.messages {
  max-width: 768px;
  width: 100%;
  margin: 0 auto;
  padding: 8px 16px 24px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

/* ========== Empty State ========== */
.chat-area.empty { justify-content: center; }
.chat-area.empty .messages { flex: 1; display: flex; }

.empty-state {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.empty-state h1 {
  font-size: 22px;
  font-weight: 600;
  color: var(--text-primary);
  text-align: center;
}

/* ========== Messages ========== */
.message {
  animation: msgFadeIn 0.3s ease;
}

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

/* User message */
.message-user {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  padding: 8px 0;
}

.message-bubble {
  background: linear-gradient(135deg, #7c3aed, #6d28d9);
  border-radius: var(--radius-lg);
  padding: 10px 18px;
  max-width: 85%;
  font-size: 16px;
  line-height: 1.6;
  word-break: break-word;
  color: #fff;
}

.message-user-actions {
  display: flex;
  gap: 2px;
  margin-top: 6px;
  padding-right: 4px;
}

.msg-action-btn {
  width: 28px; height: 28px;
  display: flex; align-items: center; justify-content: center;
  background: none; border: none;
  color: var(--text-hint);
  cursor: pointer;
  border-radius: 6px;
}
.msg-action-btn svg { width: 16px; height: 16px; }

/* Assistant message */
.message-assistant {
  padding: 8px 0 16px;
}

.thinking-label {
  font-size: 13px;
  color: var(--text-tertiary);
  margin-bottom: 10px;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 4px 10px 4px 6px;
  border-radius: var(--radius-full);
  border: 1px solid var(--border-subtle);
  background: rgba(255,255,255,0.03);
  cursor: pointer;
  transition: background 0.15s;
}
.thinking-label:hover { background: rgba(255,255,255,0.06); }

.thinking-label .think-icon {
  width: 14px; height: 14px;
  display: inline-flex;
  flex-shrink: 0;
}

.thinking-label .think-icon svg {
  width: 14px; height: 14px;
  animation: none;
}

.thinking-label.animating .think-icon svg {
  animation: spin 1.5s linear infinite;
}

.thinking-label .think-chevron {
  width: 10px; height: 10px;
  display: inline-flex;
  opacity: 0.5;
  margin-left: 2px;
}
.thinking-label .think-chevron svg { width: 10px; height: 10px; }

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

.assistant-content {
  font-size: 16px;
  line-height: 1.75;
  color: var(--text-primary);
}

.assistant-content p { margin-bottom: 14px; }
.assistant-content p:last-child { margin-bottom: 0; }

.assistant-content h4 {
  font-size: 16px;
  font-weight: 700;
  margin: 20px 0 8px;
  color: var(--text-primary);
}

.assistant-content h4:first-child { margin-top: 4px; }

.assistant-content ul {
  padding-left: 20px;
  margin-bottom: 14px;
}
.assistant-content li {
  margin-bottom: 4px;
  color: var(--text-primary);
}

.assistant-actions {
  display: flex;
  gap: 2px;
  margin-top: 12px;
}

/* Typing indicator */
.typing-dots {
  display: flex;
  gap: 4px;
  padding: 8px 0;
}
.typing-dots span {
  width: 6px; height: 6px;
  border-radius: 50%;
  background: var(--text-tertiary);
  animation: dotBlink 1.4s infinite both;
}
.typing-dots span:nth-child(2) { animation-delay: 0.2s; }
.typing-dots span:nth-child(3) { animation-delay: 0.4s; }

@keyframes dotBlink {
  0%, 80%, 100% { opacity: 0.3; }
  40% { opacity: 1; }
}

/* ========== Input Area ========== */
.input-area {
  padding: 0 12px;
  padding-bottom: calc(8px + var(--safe-bottom));
  flex-shrink: 0;
  max-width: 768px;
  width: 100%;
  margin: 0 auto;
}

.input-container {
  background: var(--bg-input);
  border: 1px solid var(--border-input);
  border-radius: var(--radius-lg);
  padding: 0 16px;
  transition: border-color 0.15s;
}

.input-container:focus-within {
  border-color: rgba(255,255,255,0.2);
}

.input-box {
  width: 100%;
  background: none;
  border: none;
  color: var(--text-primary);
  font-size: 16px;
  font-family: inherit;
  line-height: 1.5;
  padding: 14px 0;
  outline: none;
  resize: none;
  max-height: 160px;
  overflow-y: auto;
}

.input-box::placeholder {
  color: var(--text-hint);
}

.input-toolbar {
  display: flex;
  align-items: center;
  padding: 0 0 10px;
  gap: 6px;
}

.tool-btn {
  width: 34px; height: 34px;
  display: flex; align-items: center; justify-content: center;
  background: none;
  border: 1px solid var(--border-input);
  color: var(--text-secondary);
  border-radius: var(--radius-full);
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.15s;
}
.tool-btn:hover { background: var(--bg-hover); }
.tool-btn svg { width: 18px; height: 18px; }

.thinking-toggle {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 6px 0 6px 8px;
  border: 1px solid rgba(100,116,139,0.45);
  border-radius: var(--radius-full);
  font-size: 14px;
  font-weight: 500;
  color: #fff;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.15s;
  background: rgba(71,85,105,0.55);
}
.thinking-toggle:hover { background: rgba(71,85,105,0.7); }

.thinking-toggle .toggle-icon {
  width: 18px; height: 18px;
  display: flex; align-items: center; justify-content: center;
  color: #fff;
}
.thinking-toggle .toggle-icon svg { width: 16px; height: 16px; }

.thinking-toggle .toggle-arrow {
  display: flex; align-items: center; justify-content: center;
  width: 14px; height: 14px;
  opacity: 0.7;
}
.thinking-toggle .toggle-arrow svg { width: 12px; height: 12px; }

.thinking-toggle .toggle-close {
  display: flex; align-items: center; justify-content: center;
  width: 28px; height: 28px;
  margin-left: 2px;
  opacity: 0.6;
  font-size: 15px;
  line-height: 1;
  border-left: 1px solid rgba(100,116,139,0.4);
  transition: opacity 0.15s;
}
.thinking-toggle .toggle-close:hover { opacity: 1; }

.toolbar-spacer { flex: 1; }

.send-btn {
  width: 34px; height: 34px;
  border-radius: 50%;
  border: none;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: opacity 0.15s, background 0.15s;
}

.send-btn.ready {
  background: #fff;
  color: var(--bg-main);
}

.send-btn.idle {
  background: linear-gradient(135deg, var(--accent-purple), #a78bfa);
  color: #fff;
}

.send-btn:disabled {
  opacity: 0.3;
  cursor: default;
}

.send-btn svg { width: 18px; height: 18px; }

.input-footer {
  text-align: center;
  font-size: 12px;
  color: var(--text-hint);
  padding: 8px 0 2px;
  line-height: 1.4;
}

/* ========== Scrollbar ========== */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.12); border-radius: 2px; }

/* ========== Desktop (min-width: 768px) ========== */
@media (min-width: 768px) {
  .sidebar {
    position: relative;
    transform: none;
    border-right: 1px solid var(--border-subtle);
    width: 260px;
  }
  .sidebar-overlay { display: none; }
  .topbar .menu-btn { display: none; }

  .messages { padding: 8px 24px 24px; }
  .input-area { padding: 0 24px; padding-bottom: 16px; }

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

/* ========== Utility ========== */
.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip: rect(0,0,0,0);
  border: 0;
}
