* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --primary: #6366f1;
  --primary-dark: #4f46e5;
  --secondary: #64748b;
  --bg: #0f172a;
  --surface: #1e293b;
  --surface-light: #334155;
  --text: #f8fafc;
  --text-muted: #94a3b8;
  --border: #334155;
  --user-bubble: #4f46e5;
  --ai-bubble: #1e293b;
  --error: #ef4444;
  --success: #22c55e;
  --shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.3);
}

body[data-theme="light"] {
  --primary: #4f46e5;
  --primary-dark: #4338ca;
  --secondary: #64748b;
  --bg: #f8fafc;
  --surface: #ffffff;
  --surface-light: #e2e8f0;
  --text: #0f172a;
  --text-muted: #64748b;
  --border: #e2e8f0;
  --user-bubble: #4f46e5;
  --ai-bubble: #f1f5f9;
  --error: #dc2626;
  --success: #16a34a;
  --shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.08);
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background-color: var(--bg);
  color: var(--text);
  line-height: 1.6;
}

.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  max-width: 1200px;
  margin: 0 auto;
}

/* Header */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.5rem;
  background-color: var(--surface);
  border-bottom: 1px solid var(--border);
  gap: 1rem;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  min-width: 0;
}

.logo {
  font-size: 1.75rem;
  flex-shrink: 0;
}

.header h1 {
  font-size: 1.25rem;
  font-weight: 600;
}

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

.header-right {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-shrink: 0;
}

.model-select {
  padding: 0.5rem 0.75rem;
  border-radius: 0.5rem;
  border: 1px solid var(--border);
  background-color: var(--bg);
  color: var(--text);
  font-size: 0.875rem;
  cursor: pointer;
}

.model-select:focus {
  outline: none;
  border-color: var(--primary);
}

/* Buttons */
.btn {
  padding: 0.5rem 1rem;
  border: none;
  border-radius: 0.5rem;
  font-size: 0.875rem;
  cursor: pointer;
  transition: all 0.2s;
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
}

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

.btn-primary {
  background-color: var(--primary);
  color: white;
}

.btn-primary:hover:not(:disabled) {
  background-color: var(--primary-dark);
}

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

.btn-secondary:hover:not(:disabled) {
  background-color: #475569;
}

/* Main chat area */
.chat-container {
  flex: 1;
  overflow: hidden;
  position: relative;
}

.loading-screen,
.error-screen {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2rem;
  text-align: center;
}

.loading-content,
.error-content {
  max-width: 400px;
}

.spinner {
  width: 48px;
  height: 48px;
  border: 4px solid var(--surface-light);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin: 0 auto 1rem;
}

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

#loading-text {
  font-size: 1.1rem;
  font-weight: 500;
  margin-bottom: 0.25rem;
}

#loading-detail {
  color: var(--text-muted);
  font-size: 0.875rem;
  margin-bottom: 1rem;
}

.progress-bar {
  width: 100%;
  height: 6px;
  background-color: var(--surface-light);
  border-radius: 3px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  width: 0%;
  background-color: var(--primary);
  transition: width 0.3s ease;
}

.error-screen h2 {
  color: var(--error);
  margin-bottom: 0.5rem;
}

.error-screen p {
  color: var(--text-muted);
  margin-bottom: 1rem;
}

/* Chat messages */
.chat-messages {
  height: 100%;
  overflow-y: auto;
  padding: 1.5rem;
  scroll-behavior: smooth;
}

.welcome-message {
  max-width: 600px;
  margin: 0 auto 2rem;
  text-align: center;
  padding: 1.5rem;
  background-color: var(--surface);
  border-radius: 1rem;
  border: 1px solid var(--border);
}

.welcome-message h2 {
  margin-bottom: 0.5rem;
}

.welcome-message p {
  color: var(--text-muted);
  margin-bottom: 0.5rem;
}

.welcome-message .note {
  font-size: 0.8rem;
  color: var(--secondary);
}

.message {
  display: flex;
  gap: 0.75rem;
  margin-bottom: 1.25rem;
  animation: fadeIn 0.3s ease;
}

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

.message.user {
  flex-direction: row-reverse;
}

.message-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  flex-shrink: 0;
  background-color: var(--surface-light);
}

.message.user .message-avatar {
  background-color: var(--user-bubble);
}

.message-content {
  max-width: 80%;
  padding: 0.875rem 1rem;
  border-radius: 1rem;
  background-color: var(--ai-bubble);
  border: 1px solid var(--border);
  white-space: pre-wrap;
  word-wrap: break-word;
}

.message.user .message-content {
  background-color: var(--user-bubble);
  border-color: var(--primary-dark);
}

.message-content p {
  margin-bottom: 0.5rem;
}

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

.message-content code {
  background-color: rgba(0, 0, 0, 0.3);
  padding: 0.125rem 0.25rem;
  border-radius: 0.25rem;
  font-family: monospace;
  font-size: 0.9em;
}

.message-content pre {
  background-color: rgba(0, 0, 0, 0.3);
  padding: 0.75rem;
  border-radius: 0.5rem;
  overflow-x: auto;
  margin: 0.5rem 0;
}

.message-content pre code {
  background: none;
  padding: 0;
}

/* Typing indicator */
.typing-indicator {
  display: flex;
  gap: 0.25rem;
  padding: 0.25rem 0;
}

.typing-indicator span {
  width: 8px;
  height: 8px;
  background-color: var(--text-muted);
  border-radius: 50%;
  animation: bounce 1.4s ease-in-out infinite both;
}

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

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

@keyframes bounce {
  0%, 80%, 100% {
    transform: scale(0);
  }
  40% {
    transform: scale(1);
  }
}

/* Input area */
.input-area {
  padding: 1rem 1.5rem;
  background-color: var(--surface);
  border-top: 1px solid var(--border);
}

.input-wrapper {
  display: flex;
  gap: 0.5rem;
  align-items: flex-end;
}

.message-input {
  flex: 1;
  padding: 0.75rem 1rem;
  border-radius: 0.75rem;
  border: 1px solid var(--border);
  background-color: var(--bg);
  color: var(--text);
  font-size: 0.95rem;
  resize: none;
  max-height: 150px;
  min-height: 44px;
  font-family: inherit;
  line-height: 1.5;
}

.message-input:focus {
  outline: none;
  border-color: var(--primary);
}

.message-input::placeholder {
  color: var(--text-muted);
}

.send-btn {
  height: 44px;
  width: 44px;
  padding: 0;
  justify-content: center;
  border-radius: 0.75rem;
  font-size: 1.1rem;
}

.status-bar {
  margin-top: 0.5rem;
  font-size: 0.75rem;
  color: var(--text-muted);
  text-align: center;
}

/* Custom model form */
.custom-model-form {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 2rem 1rem;
  overflow-y: auto;
  background-color: var(--bg);
}

.custom-model-card {
  width: 100%;
  max-width: 640px;
  background-color: var(--surface);
  border: 1px solid var(--border);
  border-radius: 1rem;
  padding: 1.5rem;
  box-shadow: var(--shadow);
}

.custom-model-card h3 {
  margin-bottom: 0.75rem;
  font-size: 1.2rem;
}

.help-text {
  color: var(--text-muted);
  font-size: 0.875rem;
  margin-bottom: 1.25rem;
  line-height: 1.6;
}

.help-text code {
  background-color: var(--bg);
  padding: 0.125rem 0.375rem;
  border-radius: 0.25rem;
  font-family: monospace;
  font-size: 0.85em;
  word-break: break-all;
}

.form-group {
  margin-bottom: 1rem;
}

.form-group label {
  display: block;
  font-size: 0.875rem;
  font-weight: 500;
  margin-bottom: 0.375rem;
  color: var(--text);
}

.form-group label .optional {
  color: var(--text-muted);
  font-weight: 400;
  font-size: 0.8rem;
}

.form-input {
  width: 100%;
  padding: 0.75rem 1rem;
  border-radius: 0.5rem;
  border: 1px solid var(--border);
  background-color: var(--bg);
  color: var(--text);
  font-size: 0.9rem;
  font-family: inherit;
}

.form-input:focus {
  outline: none;
  border-color: var(--primary);
}

.form-input::placeholder {
  color: var(--text-muted);
}

.form-actions {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 1.25rem;
}

.examples {
  padding-top: 1rem;
  border-top: 1px solid var(--border);
  font-size: 0.8rem;
  color: var(--text-muted);
}

.examples strong {
  color: var(--text);
  display: block;
  margin-bottom: 0.5rem;
}

.examples ul {
  list-style: none;
  margin-bottom: 0.75rem;
}

.examples li {
  margin-bottom: 0.25rem;
}

.examples code {
  background-color: var(--bg);
  padding: 0.125rem 0.375rem;
  border-radius: 0.25rem;
  font-family: monospace;
  word-break: break-all;
}

.examples .note {
  font-style: italic;
  line-height: 1.5;
}

/* Status bar */
.status-bar {
  display: flex;
  justify-content: center;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.model-source {
  color: var(--primary);
  font-weight: 500;
}

.model-source:empty {
  display: none;
}

/* Model selection screen */
.model-selection-screen {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 2rem 1rem;
  overflow-y: auto;
  background-color: var(--bg);
}

.selection-content {
  width: 100%;
  max-width: 900px;
}

.selection-header {
  text-align: center;
  margin-bottom: 2rem;
}

.selection-header h2 {
  font-size: 1.75rem;
  margin-bottom: 0.5rem;
}

.selection-header p {
  color: var(--text-muted);
  font-size: 1rem;
  margin-bottom: 0.5rem;
}

.selection-header .note {
  font-size: 0.85rem;
  color: var(--secondary);
  max-width: 600px;
  margin: 0 auto;
}

.model-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 1rem;
}

.model-card {
  background-color: var(--surface);
  border: 1px solid var(--border);
  border-radius: 1rem;
  padding: 1.25rem;
  cursor: pointer;
  transition: all 0.2s ease;
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.model-card:hover {
  border-color: var(--primary);
  transform: translateY(-2px);
  box-shadow: var(--shadow);
}

.model-card.custom-card {
  border-style: dashed;
  justify-content: center;
  align-items: center;
  text-align: center;
  min-height: 200px;
}

.model-card.custom-card:hover {
  border-color: var(--primary);
  background-color: var(--surface-light);
}

.model-card-badge {
  position: absolute;
  top: 0.75rem;
  right: 0.75rem;
  font-size: 0.7rem;
  padding: 0.25rem 0.5rem;
  border-radius: 9999px;
  background-color: var(--success);
  color: var(--bg);
  font-weight: 600;
}

.model-card-icon {
  font-size: 2rem;
}

.model-card h3 {
  font-size: 1.1rem;
  font-weight: 600;
}

.model-card-specs {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  font-size: 0.8rem;
}

.model-card-specs span {
  background-color: var(--bg);
  padding: 0.25rem 0.5rem;
  border-radius: 0.375rem;
  color: var(--text-muted);
}

.model-card-description {
  font-size: 0.85rem;
  color: var(--text-muted);
  flex: 1;
}

.model-card-action {
  margin-top: auto;
  padding-top: 0.5rem;
}

.model-card-action .btn {
  width: 100%;
  justify-content: center;
}

/* Init screen */
.init-screen {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2rem;
  text-align: center;
  background-color: var(--bg);
}

.init-content {
  max-width: 480px;
  width: 100%;
}

.init-prompt {
  background-color: var(--surface);
  border: 1px solid var(--border);
  border-radius: 1rem;
  padding: 2rem;
}

.init-model-icon {
  font-size: 3rem;
  margin-bottom: 0.5rem;
}

.init-prompt h2 {
  font-size: 1.5rem;
  margin-bottom: 0.25rem;
}

.init-prompt #init-model-info {
  color: var(--text-muted);
  margin-bottom: 1rem;
}

.init-note {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-bottom: 1.5rem;
  line-height: 1.5;
}

.init-actions {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.btn-large {
  padding: 0.875rem 1.5rem;
  font-size: 1rem;
  justify-content: center;
}

.init-progress {
  background-color: var(--surface);
  border: 1px solid var(--border);
  border-radius: 1rem;
  padding: 2rem;
}

.init-progress .spinner {
  margin-bottom: 1.25rem;
}

#init-loading-text {
  font-size: 1.1rem;
  font-weight: 500;
  margin-bottom: 0.25rem;
}

#init-loading-detail {
  color: var(--text-muted);
  font-size: 0.875rem;
  margin-bottom: 1rem;
}

.cancel-btn {
  margin-top: 1rem;
}

.cancel-note {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: 0.75rem;
  font-style: italic;
}

/* Error screen */
.error-actions {
  display: flex;
  gap: 0.5rem;
  justify-content: center;
  flex-wrap: wrap;
}

/* Settings panel */
.settings-panel {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  justify-content: flex-end;
}

.settings-overlay {
  position: absolute;
  inset: 0;
  background-color: rgba(0, 0, 0, 0.6);
}

.settings-content {
  position: relative;
  width: 100%;
  max-width: 420px;
  height: 100%;
  background-color: var(--surface);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  box-shadow: var(--shadow);
}

.settings-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.5rem;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background-color: var(--surface);
  z-index: 1;
}

.settings-header h2 {
  font-size: 1.25rem;
}

.settings-section {
  padding: 1.5rem;
  border-bottom: 1px solid var(--border);
}

.settings-section h3 {
  font-size: 1rem;
  margin-bottom: 0.75rem;
}

.settings-help {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-bottom: 1rem;
  line-height: 1.5;
}

.settings-info {
  font-size: 0.85rem;
  color: var(--text-muted);
  line-height: 1.8;
}

.settings-info code {
  background-color: var(--bg);
  padding: 0.125rem 0.375rem;
  border-radius: 0.25rem;
  font-family: monospace;
}

.cached-models-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  margin-bottom: 1rem;
}

.cached-model-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1rem;
  background-color: var(--bg);
  border: 1px solid var(--border);
  border-radius: 0.5rem;
}

.cached-model-info {
  display: flex;
  flex-direction: column;
  gap: 0.125rem;
}

.cached-model-name {
  font-weight: 500;
  font-size: 0.9rem;
}

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

.empty-cache {
  color: var(--text-muted);
  font-style: italic;
  font-size: 0.85rem;
  text-align: center;
  padding: 1rem;
}

.btn-danger {
  background-color: rgba(239, 68, 68, 0.2);
  color: var(--error);
  border: 1px solid var(--error);
}

.btn-danger:hover:not(:disabled) {
  background-color: rgba(239, 68, 68, 0.3);
}

.delete-cache-btn {
  padding: 0.375rem 0.625rem;
  font-size: 0.75rem;
}

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

/* Scrollbar */
.chat-messages::-webkit-scrollbar {
  width: 8px;
}

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

.chat-messages::-webkit-scrollbar-thumb {
  background-color: var(--surface-light);
  border-radius: 4px;
}

.chat-messages::-webkit-scrollbar-thumb:hover {
  background-color: var(--secondary);
}

/* Responsive */
@media (max-width: 640px) {
  .header {
    padding: 0.75rem 1rem;
  }

  .header h1 {
    font-size: 1rem;
  }

  .subtitle {
    display: none;
  }

  .header-right {
    gap: 0.25rem;
  }

  .model-select {
    max-width: 120px;
    font-size: 0.8rem;
    padding: 0.4rem 0.5rem;
  }

  .btn {
    padding: 0.4rem 0.6rem;
    font-size: 0.8rem;
  }

  .chat-messages {
    padding: 1rem;
  }

  .message-content {
    max-width: 90%;
  }

  .input-area {
    padding: 0.75rem 1rem;
  }
}
