/* ===== CSS Variables for Theming ===== */
:root {
  /* Light theme (default) */
  --bg-primary: #f8fafc;
  --bg-secondary: #ffffff;
  --bg-tertiary: #f1f5f9;
  --bg-accent: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  --text-primary: #1e293b;
  --text-secondary: #475569;
  --text-muted: #94a3b8;
  --border-color: #e2e8f0;
  --border-focus: #6366f1;
  --accent-primary: #6366f1;
  --accent-secondary: #8b5cf6;
  --accent-gradient: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%);
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.05);
  --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
  --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
  --shadow-xl: 0 20px 25px -5px rgba(0, 0, 0, 0.1);
  --user-bubble: #6366f1;
  --user-text: #ffffff;
  --assistant-bubble: #f1f5f9;
  --assistant-text: #1e293b;
  --success: #10b981;
  --error: #ef4444;
  --warning: #f59e0b;
  --radius-sm: 6px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --radius-xl: 24px;
}

[data-theme="dark"] {
  --bg-primary: #0f172a;
  --bg-secondary: #1e293b;
  --bg-tertiary: #334155;
  --bg-accent: linear-gradient(135deg, #312e81 0%, #581c87 100%);
  --text-primary: #f1f5f9;
  --text-secondary: #cbd5e1;
  --text-muted: #64748b;
  --border-color: #334155;
  --border-focus: #818cf8;
  --accent-primary: #818cf8;
  --accent-secondary: #a78bfa;
  --accent-gradient: linear-gradient(135deg, #818cf8 0%, #a78bfa 100%);
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.4);
  --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.4);
  --shadow-xl: 0 20px 25px -5px rgba(0, 0, 0, 0.4);
  --user-bubble: #6366f1;
  --user-text: #ffffff;
  --assistant-bubble: #1e293b;
  --assistant-text: #f1f5f9;
}

/* ===== 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;
}

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  line-height: 1.6;
  min-height: 100vh;
  transition: background-color 0.3s ease, color 0.3s ease;
}

/* ===== App Container ===== */
.app {
  max-width: 900px;
  margin: 0 auto;
  padding: 16px;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* ===== Header ===== */
header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 0;
  margin-bottom: 16px;
}

.title {
  display: flex;
  align-items: center;
  gap: 12px;
}

.logo-container {
  display: flex;
  align-items: center;
  justify-content: center;
}

.logo {
  width: 48px;
  height: 48px;
  border-radius: var(--radius-md);
  filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.1));
  transition: transform 0.3s ease;
}

.logo:hover {
  transform: rotate(-10deg) scale(1.1);
}

.title-text h1 {
  font-size: 1.5rem;
  font-weight: 700;
  background: linear-gradient(135deg, #FFE135 0%, #F59E0B 50%, #6366f1 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.subtitle {
  font-size: 0.875rem;
  color: var(--text-muted);
  font-weight: 400;
}

.actions {
  display: flex;
  gap: 8px;
}

.icon-btn {
  width: 40px;
  height: 40px;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  background: var(--bg-secondary);
  color: var(--text-secondary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s ease;
}

.icon-btn:hover {
  background: var(--bg-tertiary);
  border-color: var(--accent-primary);
  color: var(--accent-primary);
  transform: translateY(-1px);
}

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

/* Theme toggle icons */
.icon-sun { display: block; }
.icon-moon { display: none; }
[data-theme="dark"] .icon-sun { display: none; }
[data-theme="dark"] .icon-moon { display: block; }

/* ===== Status Bar ===== */
.status {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 12px;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  margin-bottom: 16px;
  font-size: 0.813rem;
}

.badge {
  display: inline-flex;
  align-items: center;
  padding: 4px 10px;
  border-radius: 9999px;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.025em;
  background: rgba(99, 102, 241, 0.1);
  color: var(--accent-primary);
  border: 1px solid rgba(99, 102, 241, 0.2);
}

#init-label {
  color: var(--text-secondary);
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ===== Chat Area ===== */
.chat {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  overflow: hidden;
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  min-height: 400px;
  max-height: calc(100vh - 320px);
}

/* Welcome Message */
.welcome-message {
  text-align: center;
  padding: 40px 20px;
  color: var(--text-secondary);
}

.welcome-icon {
  font-size: 3rem;
  margin-bottom: 16px;
}

.welcome-message h2 {
  font-size: 1.5rem;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 8px;
}

.welcome-message p {
  color: var(--text-muted);
  max-width: 400px;
  margin: 0 auto 24px;
}

.features {
  display: flex;
  justify-content: center;
  gap: 24px;
  flex-wrap: wrap;
}

.feature {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.875rem;
  color: var(--text-secondary);
}

.feature-icon {
  font-size: 1.25rem;
}

/* Message Bubbles */
.msg {
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-width: 85%;
  animation: fadeIn 0.3s ease;
}

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

.msg.user {
  align-self: flex-end;
}

.msg.assistant {
  align-self: flex-start;
}

.msg .who {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-muted);
  padding: 0 4px;
}

.msg.user .who {
  text-align: right;
}

.msg .bubble {
  padding: 12px 16px;
  border-radius: var(--radius-lg);
  line-height: 1.6;
  word-wrap: break-word;
}

.msg.user .bubble {
  background: var(--accent-gradient);
  color: var(--user-text);
  border-bottom-right-radius: var(--radius-sm);
}

.msg.assistant .bubble {
  background: var(--assistant-bubble);
  color: var(--assistant-text);
  border-bottom-left-radius: var(--radius-sm);
  border: 1px solid var(--border-color);
}

/* ===== Input Area ===== */
.input-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 16px;
  border-top: 1px solid var(--border-color);
  background: var(--bg-secondary);
}

.input-container {
  flex: 1;
  display: flex;
  align-items: flex-end;
  background: var(--bg-tertiary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  padding: 8px 12px;
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.input-container:focus-within {
  border-color: var(--accent-primary);
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1);
}

#prompt {
  flex: 1;
  border: none;
  background: transparent;
  color: var(--text-primary);
  font-size: 0.938rem;
  font-family: inherit;
  line-height: 1.5;
  resize: none;
  min-height: 24px;
  max-height: 150px;
  padding: 4px 0;
}

#prompt:focus {
  outline: none;
}

#prompt::placeholder {
  color: var(--text-muted);
}

#prompt:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.input-actions {
  display: flex;
  align-items: center;
  gap: 4px;
}

.input-icon-btn {
  width: 32px;
  height: 32px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  border-radius: var(--radius-sm);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s ease;
}

.input-icon-btn:hover:not(:disabled) {
  background: var(--bg-secondary);
  color: var(--accent-primary);
}

.input-icon-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.send-btn {
  width: 44px;
  height: 44px;
  border: none;
  background: var(--accent-gradient);
  color: white;
  border-radius: var(--radius-md);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s ease;
  flex-shrink: 0;
}

.send-btn:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: var(--shadow-md);
}

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

.stop-btn {
  display: none;
  align-items: center;
  gap: 6px;
  padding: 10px 16px;
  background: var(--error);
  color: white;
  border: none;
  border-radius: var(--radius-md);
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
}

.stop-btn:hover {
  background: #dc2626;
}

/* ===== File Preview ===== */
.file-preview {
  padding: 12px 16px;
  border-top: 1px solid var(--border-color);
  background: var(--bg-tertiary);
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.file-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  font-size: 0.813rem;
}

.file-icon {
  font-size: 1rem;
}

.file-info {
  display: flex;
  flex-direction: column;
}

.file-name {
  font-weight: 500;
  color: var(--text-primary);
}

.file-size {
  font-size: 0.75rem;
  color: var(--text-muted);
}

.remove-file {
  width: 20px;
  height: 20px;
  border: none;
  background: var(--error);
  color: white;
  border-radius: 50%;
  font-size: 0.75rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s ease;
}

.remove-file:hover {
  background: #dc2626;
}

/* ===== Dialog ===== */
dialog {
  border: none;
  border-radius: var(--radius-lg);
  padding: 0;
  background: var(--bg-secondary);
  color: var(--text-primary);
  box-shadow: var(--shadow-xl);
  width: 90%;
  max-width: 480px;
  max-height: 85vh;
  overflow: hidden;
  /* Center the dialog */
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  margin: 0;
}

dialog::backdrop {
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(4px);
}

dialog[open] {
  animation: dialogIn 0.2s ease forwards;
}

/* Hide close button on initial load (before model selected) */
dialog.initial-load .dialog-close {
  display: none;
}

@keyframes dialogIn {
  from { 
    opacity: 0; 
    transform: translate(-50%, -50%) scale(0.95); 
  }
  to { 
    opacity: 1; 
    transform: translate(-50%, -50%) scale(1); 
  }
}

.dialog-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border-color);
}

.dialog-header h3 {
  font-size: 1.125rem;
  font-weight: 600;
}

.dialog-close {
  width: 32px;
  height: 32px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  border-radius: var(--radius-sm);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s ease;
}

.dialog-close:hover {
  background: var(--bg-tertiary);
  color: var(--text-primary);
}

.settings-form {
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 20px;
  max-height: calc(85vh - 60px);
  overflow-y: auto;
}

.setting-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.setting-group label {
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--text-primary);
}

.setting-hint {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin: 0;
}

.setting-group select,
.setting-group input[type="number"] {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  background: var(--bg-tertiary);
  color: var(--text-primary);
  font-size: 0.875rem;
  font-family: inherit;
  transition: border-color 0.2s ease;
}

.setting-group select:focus,
.setting-group input:focus {
  outline: none;
  border-color: var(--accent-primary);
}

.setting-group select optgroup {
  font-weight: 600;
  color: var(--text-secondary);
}

.model-description {
  padding: 12px;
  background: var(--bg-tertiary);
  border-radius: var(--radius-md);
  font-size: 0.813rem;
  color: var(--text-secondary);
  line-height: 1.5;
  border-left: 3px solid var(--accent-primary);
}

.settings-row {
  display: flex;
  gap: 16px;
}

.setting-group.half {
  flex: 1;
}

.setting-group input[type="range"] {
  width: 100%;
  height: 6px;
  border-radius: 3px;
  background: var(--bg-tertiary);
  appearance: none;
  cursor: pointer;
}

.setting-group input[type="range"]::-webkit-slider-thumb {
  appearance: none;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--accent-primary);
  cursor: pointer;
  box-shadow: var(--shadow-sm);
}

.range-value {
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--accent-primary);
}

.settings-actions {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  padding-top: 8px;
}

.btn-primary {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 20px;
  background: var(--accent-gradient);
  color: white;
  border: none;
  border-radius: var(--radius-md);
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
}

.btn-primary:hover {
  transform: translateY(-1px);
  box-shadow: var(--shadow-md);
}

.btn-primary:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* ===== Markdown in Messages ===== */
.msg .bubble h1,
.msg .bubble h2,
.msg .bubble h3 {
  margin: 16px 0 8px 0;
  font-weight: 600;
  color: inherit;
}

.msg .bubble h1 { font-size: 1.25rem; }
.msg .bubble h2 { font-size: 1.125rem; }
.msg .bubble h3 { font-size: 1rem; }

.msg .bubble p {
  margin: 8px 0;
}

.msg .bubble p:first-child { margin-top: 0; }
.msg .bubble p:last-child { margin-bottom: 0; }

.msg .bubble code {
  padding: 2px 6px;
  background: rgba(0, 0, 0, 0.1);
  border-radius: 4px;
  font-family: 'SF Mono', 'Monaco', 'Menlo', monospace;
  font-size: 0.875em;
}

[data-theme="dark"] .msg .bubble code {
  background: rgba(255, 255, 255, 0.1);
}

.msg .bubble pre {
  margin: 12px 0;
  padding: 12px 16px;
  background: #1e293b;
  border-radius: var(--radius-md);
  overflow-x: auto;
}

.msg .bubble pre code {
  padding: 0;
  background: transparent;
  color: #e2e8f0;
  font-size: 0.813rem;
  line-height: 1.6;
}

.msg .bubble ul,
.msg .bubble ol {
  margin: 8px 0;
  padding-left: 24px;
}

.msg .bubble li {
  margin: 4px 0;
}

.msg .bubble strong {
  font-weight: 600;
}

.msg .bubble em {
  font-style: italic;
}

/* ===== Loading Animation ===== */
.generating .bubble::after {
  content: "▋";
  animation: blink 1s infinite;
  color: var(--accent-primary);
}

@keyframes blink {
  0%, 50% { opacity: 1; }
  51%, 100% { opacity: 0; }
}

/* ===== Footer ===== */
.footer {
  padding: 16px;
  text-align: center;
  font-size: 0.813rem;
  color: var(--text-muted);
}

.footer a {
  color: var(--accent-primary);
  text-decoration: none;
  transition: color 0.2s ease;
}

.footer a:hover {
  text-decoration: underline;
}

/* ===== Scrollbar ===== */
.messages::-webkit-scrollbar {
  width: 6px;
}

.messages::-webkit-scrollbar-track {
  background: transparent;
}

.messages::-webkit-scrollbar-thumb {
  background: var(--border-color);
  border-radius: 3px;
}

.messages::-webkit-scrollbar-thumb:hover {
  background: var(--text-muted);
}

/* ===== Loading Progress ===== */
.loading-progress {
  width: 100%;
  height: 4px;
  background: var(--bg-tertiary);
  border-radius: 2px;
  overflow: hidden;
  margin-top: 8px;
}

.loading-progress-bar {
  height: 100%;
  background: var(--accent-gradient);
  border-radius: 2px;
  transition: width 0.3s ease;
  animation: progressPulse 2s ease-in-out infinite;
}

@keyframes progressPulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.7; }
}

/* ===== Typing Indicator ===== */
.typing-indicator {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 8px 12px;
}

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

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

@keyframes typingBounce {
  0%, 60%, 100% { transform: translateY(0); }
  30% { transform: translateY(-8px); }
}

/* ===== Toast Notifications ===== */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  padding: 12px 24px;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  font-size: 0.875rem;
  color: var(--text-primary);
  z-index: 1000;
  animation: toastIn 0.3s ease;
}

.toast.success {
  border-color: var(--success);
  background: rgba(16, 185, 129, 0.1);
}

.toast.error {
  border-color: var(--error);
  background: rgba(239, 68, 68, 0.1);
}

@keyframes toastIn {
  from { opacity: 0; transform: translateX(-50%) translateY(20px); }
  to { opacity: 1; transform: translateX(-50%) translateY(0); }
}

/* ===== Keyboard Shortcuts Hint ===== */
.kbd {
  display: inline-block;
  padding: 2px 6px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border-color);
  border-radius: 4px;
  font-family: 'SF Mono', 'Monaco', monospace;
  font-size: 0.75rem;
  color: var(--text-muted);
}

/* ===== Responsive ===== */
@media (max-width: 640px) {
  .app {
    padding: 12px;
  }
  
  header {
    flex-wrap: wrap;
    gap: 12px;
  }
  
  .title-text h1 {
    font-size: 1.25rem;
  }
  
  .subtitle {
    font-size: 0.75rem;
  }
  
  .messages {
    min-height: 300px;
    max-height: calc(100vh - 280px);
  }
  
  .msg {
    max-width: 95%;
  }
  
  .features {
    flex-direction: column;
    gap: 12px;
  }
  
  .settings-row {
    flex-direction: column;
  }
  
  dialog {
    width: 95%;
  }
}
