/* ============================================================
   巴菲特 Chatbot — Design System & Styles
   Theme: Dark + Amber/Gold accents, newspaper-inspired typography
   ============================================================ */

@import url('https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Inter:wght@300;400;500;600&family=JetBrains+Mono:wght@400;500&display=swap');

/* --- CSS Custom Properties --- */
:root {
  /* Color Palette — Dark gold theme */
  --bg-primary: #0a0a0f;
  --bg-secondary: #111118;
  --bg-tertiary: #1a1a24;
  --bg-card: #15151f;
  --bg-hover: #1e1e2a;
  --bg-input: #12121a;
  
  --accent-gold: #d4a853;
  --accent-gold-dim: #b08d3e;
  --accent-gold-bright: #f0c866;
  --accent-gold-glow: rgba(212, 168, 83, 0.15);
  --accent-amber: #e8a83e;
  --accent-warm: #c4956a;
  
  --text-primary: #e8e4dc;
  --text-secondary: #9a9690;
  --text-tertiary: #6b6660;
  --text-accent: #d4a853;
  --text-link: #c4956a;
  
  --border-subtle: rgba(212, 168, 83, 0.08);
  --border-light: rgba(212, 168, 83, 0.15);
  --border-accent: rgba(212, 168, 83, 0.3);
  
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.3);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.4);
  --shadow-lg: 0 8px 32px rgba(0,0,0,0.5);
  --shadow-glow: 0 0 20px rgba(212, 168, 83, 0.1);
  
  /* Typography */
  --font-serif: 'Noto Serif SC', 'Songti SC', 'STSong', Georgia, serif;
  --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --font-mono: 'JetBrains Mono', 'Menlo', monospace;
  
  /* Spacing */
  --space-xs: 4px;
  --space-sm: 8px;
  --space-md: 16px;
  --space-lg: 24px;
  --space-xl: 32px;
  --space-2xl: 48px;
  
  /* Radius */
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;
  --radius-xl: 20px;
  
  /* Transitions */
  --transition-fast: 150ms ease;
  --transition-normal: 250ms ease;
  --transition-slow: 400ms ease;
}

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

html {
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  width: 100vw;
  max-width: 100%;
  overflow-x: hidden;
}

body {
  font-family: var(--font-sans);
  background: var(--bg-primary);
  color: var(--text-primary);
  min-height: 100vh;
  width: 100%;
  max-width: 100%;
  overflow: hidden;
}

/* Scrollbar */
::-webkit-scrollbar {
  width: 5px;
}
::-webkit-scrollbar-track {
  background: transparent;
}
::-webkit-scrollbar-thumb {
  background: var(--border-light);
  border-radius: 3px;
}
::-webkit-scrollbar-thumb:hover {
  background: var(--accent-gold-dim);
}

/* --- App Layout --- */
#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  width: 100%;
  max-width: 960px;
  margin: 0 auto;
  position: relative;
}

/* --- Header --- */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-md) var(--space-lg);
  border-bottom: 1px solid var(--border-subtle);
  background: linear-gradient(180deg, var(--bg-secondary) 0%, var(--bg-primary) 100%);
  flex-shrink: 0;
  z-index: 10;
}

.header-brand {
  display: flex;
  align-items: center;
  gap: var(--space-md);
}

.header-avatar {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--accent-gold) 0%, var(--accent-gold-dim) 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  box-shadow: var(--shadow-glow);
  position: relative;
}

.header-avatar::after {
  content: '';
  position: absolute;
  inset: -2px;
  border-radius: 50%;
  border: 1px solid var(--accent-gold);
  opacity: 0.3;
  animation: pulse-ring 3s ease-in-out infinite;
}

@keyframes pulse-ring {
  0%, 100% { opacity: 0.3; transform: scale(1); }
  50% { opacity: 0.1; transform: scale(1.05); }
}

.header-info h1 {
  font-family: var(--font-serif);
  font-size: 1.1rem;
  font-weight: 600;
  color: var(--text-primary);
  letter-spacing: 0.5px;
}

.header-info .subtitle {
  font-size: 0.72rem;
  color: var(--text-tertiary);
  margin-top: 2px;
  letter-spacing: 0.3px;
}

.header-actions {
  display: flex;
  gap: var(--space-sm);
}

.header-btn {
  background: none;
  border: 1px solid var(--border-subtle);
  color: var(--text-secondary);
  width: 36px;
  height: 36px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  transition: all var(--transition-fast);
}

.header-btn:hover {
  border-color: var(--border-accent);
  color: var(--accent-gold);
  background: var(--accent-gold-glow);
}

/* --- Welcome Hero --- */
.welcome-hero {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: var(--space-2xl) var(--space-lg);
  text-align: center;
  flex: 1;
  min-height: 0;
  overflow-y: auto;
}

.welcome-hero.hidden {
  display: none;
}

.hero-avatar {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--accent-gold) 0%, var(--accent-gold-dim) 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 40px;
  margin-bottom: var(--space-lg);
  box-shadow: var(--shadow-glow), 0 0 60px rgba(212, 168, 83, 0.08);
  position: relative;
}

.hero-avatar::before {
  content: '';
  position: absolute;
  inset: -6px;
  border-radius: 50%;
  border: 1px solid var(--accent-gold);
  opacity: 0.15;
}

.hero-title {
  font-family: var(--font-serif);
  font-size: 1.6rem;
  font-weight: 700;
  color: var(--text-primary);
  margin-bottom: var(--space-sm);
  background: linear-gradient(135deg, var(--text-primary), var(--accent-gold));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.hero-subtitle {
  font-size: 0.88rem;
  color: var(--text-secondary);
  max-width: 420px;
  line-height: 1.6;
  margin-bottom: var(--space-xl);
}

.hero-quote {
  font-family: var(--font-serif);
  font-size: 0.92rem;
  color: var(--text-secondary);
  font-style: italic;
  max-width: 480px;
  line-height: 1.7;
  padding: var(--space-lg);
  border-left: 2px solid var(--accent-gold-dim);
  text-align: left;
  margin-bottom: var(--space-xl);
  background: var(--accent-gold-glow);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
}

.hero-quote .attribution {
  display: block;
  margin-top: var(--space-sm);
  font-style: normal;
  font-size: 0.78rem;
  color: var(--accent-gold-dim);
}

/* Suggestion chips */
.suggestion-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--space-sm);
  max-width: 520px;
  width: 100%;
}

.suggestion-chip {
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: var(--space-md);
  cursor: pointer;
  text-align: left;
  transition: all var(--transition-normal);
  display: flex;
  align-items: flex-start;
  gap: var(--space-sm);
}

.suggestion-chip:hover {
  border-color: var(--border-accent);
  background: var(--bg-hover);
  transform: translateY(-1px);
  box-shadow: var(--shadow-glow);
}

.suggestion-chip .chip-icon {
  font-size: 1.2rem;
  flex-shrink: 0;
  margin-top: 1px;
}

.suggestion-chip .chip-text {
  font-size: 0.82rem;
  color: var(--text-secondary);
  line-height: 1.4;
}

.suggestion-chip .chip-text strong {
  display: block;
  color: var(--text-primary);
  font-weight: 500;
  margin-bottom: 2px;
  font-size: 0.85rem;
}

/* --- Chat Area --- */
.chat-area {
  flex: 1;
  width: 100%;
  max-width: 100%;
  overflow-y: auto;
  overflow-x: hidden;
  padding: var(--space-lg);
  display: none;
  flex-direction: column;
  gap: var(--space-lg);
  scroll-behavior: smooth;
}

.chat-area.active {
  display: flex;
}

/* Message bubbles */
.message {
  display: flex;
  gap: var(--space-md);
  width: 100%;
  max-width: 100%;
  animation: message-in 0.35s ease-out;
}

@keyframes message-in {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.message-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
  margin-top: 2px;
}

.message.assistant .message-avatar {
  background: linear-gradient(135deg, var(--accent-gold) 0%, var(--accent-gold-dim) 100%);
  box-shadow: 0 0 12px rgba(212, 168, 83, 0.15);
}

.message.user .message-avatar {
  background: var(--bg-tertiary);
  border: 1px solid var(--border-light);
  color: var(--text-secondary);
  font-size: 13px;
}

.message-body {
  flex: 1;
  min-width: 0;
  width: 100%;
  overflow-x: hidden;
  overflow-wrap: anywhere;
  word-wrap: break-word;
  word-break: break-word;
}

.message-sender {
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--text-tertiary);
  margin-bottom: var(--space-xs);
}

.message.assistant .message-sender {
  color: var(--accent-gold-dim);
}

.message-content {
  font-family: var(--font-serif);
  font-size: 0.92rem;
  line-height: 1.8;
  color: var(--text-primary);
  word-break: break-word;
  overflow-wrap: break-word;
}

.message.user .message-content {
  font-family: var(--font-sans);
  background: var(--bg-tertiary);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: var(--space-md);
  font-size: 0.88rem;
  line-height: 1.5;
}

.message-content p {
  margin-bottom: var(--space-sm);
}

.message-content p:last-child {
  margin-bottom: 0;
}

.message-content strong {
  color: var(--accent-gold);
  font-weight: 600;
}

/* Source citations */
.message-sources {
  margin-top: var(--space-md);
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-xs);
}

.source-tag {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-family: var(--font-sans);
  font-size: 0.7rem;
  color: var(--accent-gold-dim);
  background: var(--accent-gold-glow);
  border: 1px solid rgba(212, 168, 83, 0.12);
  border-radius: 20px;
  padding: 3px 10px;
  cursor: pointer;
  transition: all var(--transition-fast);
}

.source-tag:hover {
  border-color: var(--accent-gold-dim);
  background: rgba(212, 168, 83, 0.12);
}

.source-tag .source-icon {
  font-size: 0.65rem;
}

/* Source expansion panel */
.source-detail {
  margin-top: var(--space-sm);
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm);
  padding: var(--space-md);
  font-family: var(--font-serif);
  font-size: 0.82rem;
  color: var(--text-secondary);
  line-height: 1.7;
  display: none;
  animation: fade-in 0.2s ease;
}

.source-detail.visible {
  display: block;
}

.source-detail .source-header {
  font-family: var(--font-sans);
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--accent-gold);
  margin-bottom: var(--space-sm);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

@keyframes fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

/* Typing indicator */
.typing-indicator {
  display: flex;
  gap: 4px;
  padding: var(--space-md) 0;
}

.typing-indicator span {
  width: 6px;
  height: 6px;
  background: var(--accent-gold-dim);
  border-radius: 50%;
  animation: typing-bounce 1.4s ease-in-out infinite;
}

.typing-indicator span:nth-child(2) {
  animation-delay: 0.2s;
}

.typing-indicator span:nth-child(3) {
  animation-delay: 0.4s;
}

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

/* --- Input Area --- */
.input-area {
  width: 100%;
  max-width: 100%;
  padding: var(--space-md) var(--space-lg) var(--space-lg);
  border-top: 1px solid var(--border-subtle);
  background: linear-gradient(0deg, var(--bg-secondary) 0%, var(--bg-primary) 100%);
  flex-shrink: 0;
}

.input-wrapper {
  display: flex;
  align-items: flex-end;
  gap: var(--space-sm);
  background: var(--bg-input);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  padding: var(--space-sm) var(--space-sm) var(--space-sm) var(--space-md);
  transition: border-color var(--transition-fast);
  width: 100%;
}

.input-wrapper:focus-within {
  border-color: var(--accent-gold-dim);
  box-shadow: 0 0 0 3px var(--accent-gold-glow);
}

#chat-input {
  flex: 1;
  min-width: 0;
  width: 100%;
  background: none;
  border: none;
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: 0.9rem;
  line-height: 1.5;
  resize: none;
  min-height: 24px;
  max-height: 120px;
  padding: var(--space-sm) 0;
  outline: none;
}

#chat-input::placeholder {
  color: var(--text-tertiary);
}

.send-btn {
  width: 36px;
  height: 36px;
  border-radius: var(--radius-sm);
  background: var(--accent-gold);
  border: none;
  color: var(--bg-primary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  transition: all var(--transition-fast);
  flex-shrink: 0;
}

.send-btn:hover {
  background: var(--accent-gold-bright);
  box-shadow: 0 0 12px rgba(212, 168, 83, 0.3);
}

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

.input-hint {
  text-align: center;
  font-size: 0.68rem;
  color: var(--text-tertiary);
  margin-top: var(--space-sm);
}

.input-hint a {
  color: var(--accent-gold-dim);
  text-decoration: none;
}

/* --- Settings Modal --- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(4px);
  z-index: 100;
  display: none;
  align-items: center;
  justify-content: center;
  animation: fade-in 0.2s ease;
}

.modal-overlay.active {
  display: flex;
}

.modal {
  background: var(--bg-secondary);
  border: 1px solid var(--border-light);
  border-radius: var(--radius-xl);
  width: 90%;
  max-width: 480px;
  max-height: 85vh;
  overflow-y: auto;
  box-shadow: var(--shadow-lg);
  animation: modal-in 0.3s ease;
}

@keyframes modal-in {
  from {
    opacity: 0;
    transform: scale(0.95) translateY(10px);
  }
  to {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
}

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

.modal-header h2 {
  font-family: var(--font-serif);
  font-size: 1.1rem;
  font-weight: 600;
}

.modal-close {
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 1.2rem;
  cursor: pointer;
  padding: var(--space-xs);
  transition: color var(--transition-fast);
}

.modal-close:hover {
  color: var(--text-primary);
}

.modal-body {
  padding: var(--space-lg);
}

.form-group {
  margin-bottom: var(--space-lg);
}

.form-group label {
  display: block;
  font-size: 0.82rem;
  font-weight: 500;
  color: var(--text-secondary);
  margin-bottom: var(--space-sm);
}

.form-group select,
.form-group input[type="text"],
.form-group input[type="password"] {
  width: 100%;
  padding: var(--space-sm) var(--space-md);
  background: var(--bg-input);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: 0.88rem;
  outline: none;
  transition: border-color var(--transition-fast);
}

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

.form-group select option {
  background: var(--bg-secondary);
  color: var(--text-primary);
}

.form-group .hint {
  font-size: 0.72rem;
  color: var(--text-tertiary);
  margin-top: var(--space-xs);
}

.form-group .api-test {
  display: inline-flex;
  align-items: center;
  gap: var(--space-xs);
  margin-top: var(--space-sm);
  font-size: 0.75rem;
  color: var(--accent-gold-dim);
  cursor: pointer;
  background: none;
  border: none;
  font-family: var(--font-sans);
}

.form-group .api-test:hover {
  color: var(--accent-gold);
}

.modal-footer {
  padding: var(--space-md) var(--space-lg) var(--space-lg);
  display: flex;
  gap: var(--space-sm);
  justify-content: flex-end;
}

.btn {
  padding: var(--space-sm) var(--space-lg);
  border-radius: var(--radius-sm);
  font-family: var(--font-sans);
  font-size: 0.82rem;
  font-weight: 500;
  cursor: pointer;
  transition: all var(--transition-fast);
  border: 1px solid;
}

.btn-primary {
  background: var(--accent-gold);
  color: var(--bg-primary);
  border-color: var(--accent-gold);
}

.btn-primary:hover {
  background: var(--accent-gold-bright);
}

.btn-secondary {
  background: none;
  color: var(--text-secondary);
  border-color: var(--border-light);
}

.btn-secondary:hover {
  color: var(--text-primary);
  border-color: var(--border-accent);
}

/* --- Status Bar --- */
.status-bar {
  display: flex;
  align-items: center;
  gap: var(--space-xs);
  font-size: 0.68rem;
  color: var(--text-tertiary);
  padding: 0 var(--space-lg);
  margin-bottom: var(--space-xs);
}

.status-dot {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: #4caf50;
}

.status-dot.offline {
  background: var(--text-tertiary);
}

.status-dot.warning {
  background: var(--accent-amber);
}

/* --- Responsive --- */
@media (max-width: 640px) {
  .app-header {
    padding: var(--space-sm) var(--space-md);
  }
  
  .chat-area {
    padding: var(--space-md);
  }
  
  .input-area {
    padding: var(--space-sm) var(--space-md) var(--space-md);
  }
  
  .hero-title {
    font-size: 1.3rem;
  }
  
  .suggestion-grid {
    grid-template-columns: 1fr;
  }
  
  .welcome-hero {
    padding: var(--space-lg) var(--space-md);
  }

  .hero-quote {
    font-size: 0.85rem;
    padding: var(--space-md);
  }
}

/* --- Utility --- */
.hidden {
  display: none !important;
}

/* Markdown rendering support */
.message-content blockquote {
  border-left: 2px solid var(--accent-gold-dim);
  padding-left: var(--space-md);
  margin: var(--space-sm) 0;
  color: var(--text-secondary);
  font-style: italic;
}

.message-content code {
  font-family: var(--font-mono);
  background: var(--bg-tertiary);
  padding: 1px 5px;
  border-radius: 3px;
  font-size: 0.85em;
}

.message-content ul, .message-content ol {
  padding-left: var(--space-lg);
  margin: var(--space-sm) 0;
}

.message-content li {
  margin-bottom: var(--space-xs);
}

/* Loading shimmer */
.shimmer {
  background: linear-gradient(90deg, var(--bg-tertiary) 25%, var(--bg-hover) 50%, var(--bg-tertiary) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.5s infinite;
  border-radius: var(--radius-sm);
}

@keyframes shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}
