/* CSS Variables for Themes */
:root {
  --primary-gradient: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%);
  --primary-color: #6366f1;
  --primary-hover: #4f46e5;
  
  /* Dark Theme (Default) */
  --bg-app: #090a0f;
  --bg-sidebar: #11131c;
  --bg-card: #181b28;
  --bg-input: #1b1e2e;
  --border-color: rgba(255, 255, 255, 0.08);
  --text-main: #f3f4f6;
  --text-muted: #9ca3af;
  --shadow-main: rgba(0, 0, 0, 0.4);
  --msg-user: #1f2335;
  --msg-assistant: transparent;
  --scrollbar-thumb: #2d3147;
  --bg-thinking: rgba(99, 102, 241, 0.05);
  --border-thinking: rgba(99, 102, 241, 0.2);
}

html.light {
  /* Light Theme */
  --bg-app: #f9fafb;
  --bg-sidebar: #f3f4f6;
  --bg-card: #ffffff;
  --bg-input: #ffffff;
  --border-color: rgba(0, 0, 0, 0.08);
  --text-main: #1f2937;
  --text-muted: #6b7280;
  --shadow-main: rgba(0, 0, 0, 0.05);
  --msg-user: #e5e7eb;
  --msg-assistant: transparent;
  --scrollbar-thumb: #d1d5db;
  --bg-thinking: rgba(0, 0, 0, 0.02);
  --border-thinking: rgba(0, 0, 0, 0.08);
}

/* Global Reset & Base Styles */
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: 'Inter', sans-serif;
  background-color: var(--bg-app);
  color: var(--text-main);
  height: 100vh;
  overflow: hidden;
  transition: background-color 0.3s ease, color 0.3s ease;
}

/* Layout */
.app-container {
  display: flex;
  height: 100vh;
  width: 100vw;
  position: relative;
  overflow: hidden;
}

/* Sidebar Styling */
.sidebar {
  width: 260px;
  background-color: var(--bg-sidebar);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  height: 100%;
  z-index: 10;
  transition: transform 0.3s cubic-bezier(0.16, 1, 0.3, 1), background-color 0.3s ease, border-color 0.3s ease;
}

.sidebar-header {
  padding: 1.25rem;
  border-bottom: 1px solid var(--border-color);
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  font-size: 1.2rem;
  font-weight: 700;
  background: var(--primary-gradient);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  margin-bottom: 1rem;
}

.brand-icon {
  background: var(--primary-gradient);
  -webkit-background-clip: initial;
  -webkit-text-fill-color: #ffffff;
  padding: 0.35rem 0.5rem;
  border-radius: 6px;
  font-size: 0.9rem;
}

.new-chat-btn {
  width: 100%;
  padding: 0.75rem;
  border: 1px solid var(--border-color);
  border-radius: 8px;
  background-color: var(--bg-card);
  color: var(--text-main);
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  transition: all 0.2s ease;
}

.new-chat-btn:hover {
  background-color: var(--border-color);
  border-color: var(--primary-color);
  transform: translateY(-1px);
}

.chat-history {
  flex: 1;
  overflow-y: auto;
  padding: 1.25rem;
}

.section-title {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  margin-bottom: 0.75rem;
}

.history-list {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.history-item {
  padding: 0.65rem 0.75rem;
  border-radius: 6px;
  font-size: 0.85rem;
  color: var(--text-main);
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  transition: all 0.15s ease;
}

.history-item:hover {
  background-color: var(--border-color);
}

.history-item.active {
  background-color: rgba(99, 102, 241, 0.15);
  border-left: 3px solid var(--primary-color);
  font-weight: 500;
}

.sidebar-footer {
  padding: 1.25rem;
  border-top: 1px solid var(--border-color);
}

.theme-toggle {
  width: 100%;
  padding: 0.65rem;
  border: 1px solid var(--border-color);
  border-radius: 6px;
  background-color: transparent;
  color: var(--text-main);
  font-size: 0.85rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  transition: all 0.2s ease;
}

.theme-toggle:hover {
  background-color: var(--border-color);
}

html.dark .icon-light,
html.light .icon-dark {
  display: none;
}

/* Main Chat Area */
.chat-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  height: 100%;
  position: relative;
  min-width: 0; /* CRITICAL: Allows flex items to shrink below content size on mobile */
  overflow: hidden;
}

/* Header */
.chat-header {
  height: 60px;
  padding: 0 1.5rem;
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: space-between;
  background-color: rgba(9, 10, 15, 0.8);
  backdrop-filter: blur(12px);
  z-index: 5;
}

html.light .chat-header {
  background-color: rgba(249, 250, 251, 0.8);
}

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

.menu-toggle-btn {
  display: none;
  background: none;
  border: none;
  color: var(--text-main);
  font-size: 1.2rem;
  cursor: pointer;
}

/* Model Selector */
.model-selector-container {
  position: relative;
}

.model-select-btn {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  color: var(--text-main);
  padding: 0.5rem 1rem;
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  transition: all 0.2s ease;
}

.model-select-btn:hover {
  border-color: var(--primary-color);
  box-shadow: 0 0 10px rgba(99, 102, 241, 0.2);
}

.model-dropdown {
  position: absolute;
  top: calc(100% + 8px);
  left: 0;
  width: 280px;
  background-color: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 0.5rem;
  box-shadow: 0 10px 25px var(--shadow-main);
  display: none;
  flex-direction: column;
  gap: 0.25rem;
  z-index: 100;
  animation: dropdownFadeIn 0.2s cubic-bezier(0.16, 1, 0.3, 1);
}

.model-dropdown.show {
  display: flex;
}

.dropdown-item {
  padding: 0.75rem;
  border-radius: 6px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
  transition: all 0.15s ease;
}

.dropdown-item:hover {
  background-color: var(--border-color);
  transform: translateX(2px);
}

.dropdown-item.active {
  background-color: rgba(99, 102, 241, 0.1);
}

.model-title {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-main);
  margin-bottom: 0.15rem;
}

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

.check-icon {
  color: var(--primary-color);
  display: none;
}

.dropdown-item.active .check-icon {
  display: block;
}

.new-chat-btn-header {
  background: none;
  border: none;
  color: var(--text-main);
  font-size: 1.2rem;
  cursor: pointer;
  opacity: 0.8;
  transition: opacity 0.2s, transform 0.2s;
}

.new-chat-btn-header:hover {
  opacity: 1;
  transform: scale(1.1);
}

/* Messages List */
.messages-container {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
  min-width: 0;
}

/* Welcome Screen */
.welcome-screen {
  margin: auto;
  max-width: 580px;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1.5rem;
  padding: 2rem 1rem;
  animation: fadeIn 0.8s ease;
}

.welcome-logo {
  width: 64px;
  height: 64px;
  border-radius: 16px;
  background: var(--primary-gradient);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2rem;
  color: #ffffff;
  box-shadow: 0 8px 24px rgba(99, 102, 241, 0.3);
  animation: float 4s ease-in-out infinite;
}

.welcome-screen h1 {
  font-size: 2rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.welcome-screen .subtitle {
  color: var(--text-muted);
  font-size: 0.95rem;
  line-height: 1.5;
}

.quick-prompts {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.75rem;
  width: 100%;
  margin-top: 1rem;
}

@media (min-width: 640px) {
  .quick-prompts {
    grid-template-columns: 1fr 1fr;
  }
}

.prompt-card {
  background-color: var(--bg-card);
  border: 1px solid var(--border-color);
  padding: 1rem;
  border-radius: 10px;
  text-align: left;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  align-items: center;
  transition: all 0.25s cubic-bezier(0.16, 1, 0.3, 1);
  animation: slideUp 0.5s ease;
  animation-fill-mode: both;
}

.prompt-card:nth-child(1) { animation-delay: 0.1s; }
.prompt-card:nth-child(2) { animation-delay: 0.2s; }
.prompt-card:nth-child(3) { animation-delay: 0.3s; }

.prompt-card:hover {
  border-color: var(--primary-color);
  transform: translateY(-4px);
  box-shadow: 0 8px 20px rgba(99, 102, 241, 0.15);
}

.prompt-text {
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--text-main);
  line-height: 1.4;
  padding-right: 0.5rem;
}

.prompt-icon {
  font-size: 0.8rem;
  color: var(--text-muted);
  opacity: 0;
  transition: opacity 0.2s, color 0.2s;
}

.prompt-card:hover .prompt-icon {
  opacity: 1;
  color: var(--primary-color);
}

/* Chat Messages */
.message-row {
  display: flex;
  width: 100%;
  margin: 0 auto;
  max-width: 720px;
  min-width: 0; /* CRITICAL: Allows shrinking inside the flex container */
}

.message-row.user {
  justify-content: flex-end;
}

.message {
  max-width: 85%;
  padding: 1rem 1.25rem;
  border-radius: 12px;
  line-height: 1.6;
  font-size: 0.95rem;
  position: relative;
  word-wrap: break-word;
  min-width: 0; /* CRITICAL: Prevents stretching of message bubble */
  animation: messageSlideIn 0.4s cubic-bezier(0.16, 1, 0.3, 1);
}

@media (max-width: 640px) {
  .message {
    max-width: 92%;
  }
}

.message-row.user .message {
  background-color: var(--msg-user);
  border-bottom-right-radius: 4px;
}

.message-row.assistant .message {
  background-color: var(--msg-assistant);
  border-bottom-left-radius: 4px;
  padding-left: 0;
  width: 100%;
}

/* Thinking Process Block */
.thinking-block {
  background-color: var(--bg-thinking);
  border: 1px solid var(--border-thinking);
  border-radius: 8px;
  margin-bottom: 1rem;
  overflow: hidden;
  transition: all 0.3s ease;
  width: 100%;
}

.thinking-header {
  padding: 0.75rem 1rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  cursor: pointer;
  user-select: none;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--primary-color);
  background-color: rgba(99, 102, 241, 0.02);
  transition: background-color 0.2s;
}

.thinking-header:hover {
  background-color: rgba(99, 102, 241, 0.05);
}

.thinking-status {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.thinking-icon {
  animation: spin 2s linear infinite;
  color: var(--primary-color);
}

.thinking-icon.done {
  animation: none;
  color: #10b981;
}

.thinking-chevron {
  transition: transform 0.3s cubic-bezier(0.16, 1, 0.3, 1);
}

.thinking-block.collapsed .thinking-chevron {
  transform: rotate(-90deg);
}

.thinking-content {
  padding: 1rem;
  font-size: 0.85rem;
  color: var(--text-muted);
  border-top: 1px solid var(--border-thinking);
  line-height: 1.6;
  white-space: pre-wrap;
  display: block;
}

.thinking-block.collapsed .thinking-content {
  display: none;
}

/* Markdown styling inside messages */
.message p {
  margin-bottom: 0.75rem;
}

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

.message code {
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.85rem;
  background-color: rgba(255, 255, 255, 0.1);
  padding: 0.15rem 0.35rem;
  border-radius: 4px;
  word-break: break-all;
}

html.light .message code {
  background-color: rgba(0, 0, 0, 0.05);
}

/* Code Blocks & Mobile Wrapping Fixes */
.message pre {
  margin: 1rem 0;
  border-radius: 8px;
  overflow-x: auto; /* Allow horizontal scrolling */
  max-width: 100%;
  width: 100%;
  display: block;
}

.code-container {
  position: relative;
  margin: 1rem 0;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid var(--border-color);
  max-width: 100%;
  width: 100%;
}

.code-header {
  background-color: #1a1a26;
  padding: 0.5rem 1rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.75rem;
  color: #a6adc8;
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
  user-select: none;
}

.copy-code-btn {
  background: none;
  border: none;
  color: #a6adc8;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 0.25rem;
  transition: color 0.2s;
}

.copy-code-btn:hover {
  color: #ffffff;
}

.message pre code {
  display: block;
  padding: 1rem;
  overflow-x: auto; /* CRITICAL: Enable scroll inside code block */
  max-width: 100%;
  width: 100%;
  background-color: #0f0f17 !important;
  font-size: 0.85rem;
  line-height: 1.5;
  white-space: pre; /* Keep standard formatting, but scrollable */
}

/* Typing Indicator */
.typing-indicator {
  display: flex;
  gap: 0.35rem;
  align-items: center;
  padding: 0.5rem 0;
}

.typing-dot {
  width: 6px;
  height: 6px;
  background-color: var(--text-main);
  border-radius: 50%;
  opacity: 0.4;
  animation: pulse 1.2s infinite ease-in-out;
}

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

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

/* Input Area */
.chat-input-area {
  padding: 1.5rem;
  background-color: transparent;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
  width: 100%;
}

@media (max-width: 640px) {
  .chat-input-area {
    padding: 0.75rem;
  }
}

.input-wrapper {
  width: 100%;
  max-width: 720px;
  background-color: var(--bg-input);
  border: 1px solid var(--border-color);
  border-radius: 16px;
  display: flex;
  align-items: flex-end;
  padding: 0.75rem 1rem;
  gap: 0.75rem;
  box-shadow: 0 8px 24px var(--shadow-main);
  transition: border-color 0.2s, box-shadow 0.2s;
}

.input-wrapper:focus-within {
  border-color: var(--primary-color);
  box-shadow: 0 8px 24px rgba(99, 102, 241, 0.15);
}

.message-input {
  flex: 1;
  background: none;
  border: none;
  color: var(--text-main);
  font-family: inherit;
  font-size: 0.95rem;
  resize: none;
  max-height: 200px;
  outline: none;
  padding: 0.25rem 0;
  line-height: 1.5;
}

.send-btn {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--primary-gradient);
  border: none;
  color: #ffffff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: opacity 0.2s, transform 0.2s;
}

.send-btn:disabled {
  background: var(--border-color);
  color: var(--text-muted);
  cursor: not-allowed;
}

.send-btn:not(:disabled):hover {
  transform: scale(1.05);
  box-shadow: 0 4px 12px rgba(99, 102, 241, 0.4);
}

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

/* Animations */
@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

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

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

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

@keyframes float {
  0% { transform: translateY(0px); }
  50% { transform: translateY(-6px); }
  100% { transform: translateY(0px); }
}

@keyframes pulse {
  0%, 100% { transform: scale(1); opacity: 0.4; }
  50% { transform: scale(1.3); opacity: 1; }
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* Custom Scrollbars */
.chat-history::-webkit-scrollbar,
.messages-container::-webkit-scrollbar,
.message pre::-webkit-scrollbar {
  width: 6px;
  height: 6px; /* Horizontal scrollbar height */
}

.chat-history::-webkit-scrollbar-track,
.messages-container::-webkit-scrollbar-track,
.message pre::-webkit-scrollbar-track {
  background: transparent;
}

.chat-history::-webkit-scrollbar-thumb,
.messages-container::-webkit-scrollbar-thumb,
.message pre::-webkit-scrollbar-thumb {
  background-color: var(--scrollbar-thumb);
  border-radius: 10px;
}

/* Responsive Styles */
@media (max-width: 768px) {
  .sidebar {
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    transform: translateX(-100%);
  }
  
  .sidebar.show {
    transform: translateX(0);
    box-shadow: 10px 0 30px rgba(0,0,0,0.5);
  }
  
  .sidebar-overlay {
    display: none;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(4px);
    z-index: 9;
    animation: fadeIn 0.2s ease;
  }
  
  .sidebar-overlay.show {
    display: block;
  }
  
  .menu-toggle-btn {
    display: block;
  }
}
