:root {
  --primary: #2d3748;
  --secondary: #4a5568;
  --accent: #6366f1;
  --light: #f7fafc;
  /* New variables for Dark Mode compatibility */
  --bg-container: white;
  --text-color: var(--primary);
  --border-color: #e2e8f0;
}

body.dark-mode {
  --primary: #f7fafc; /* Light text color */
  --secondary: #a0aec0; /* Lighter secondary elements color */
  --accent: #8b92f7; /* Adjusted accent */
  --light: #1a202c; /* Page Background */
  --bg-container: #2d3748; /* Chat container background */
  --border-color: #4a5568; /* Dark mode border color */
}

/* Dark mode: make chat container darker but still slightly translucent over the image */
.dark-mode .chat-container {
  background: rgba(16,18,20,0.75);
  backdrop-filter: blur(6px) saturate(110%);
}

/* Custom Scrollbar Styles for Webkit browsers (Chrome, Safari, Edge) */
::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

/* Scrollbar Track (the background area) */
::-webkit-scrollbar-track {
  background: var(--light); /* Now respects dark mode */
}

/* Scrollbar Handle (the draggable part) */
::-webkit-scrollbar-thumb {
  background: var(--accent); /* Uses accent color */
  border-radius: 10px;
}

/* Handle on hover */
::-webkit-scrollbar-thumb:hover {
  background: var(--primary); /* Darker color on hover (adaptive) */
}

body {
  font-family: "Noto Sans", system-ui, -apple-system, sans-serif;
  margin: 0;
  padding: 0;
  /* Background: Amazon landscape image with subtle dark overlay for legibility */
  background-image: linear-gradient(rgba(0,0,0,0.28), rgba(0,0,0,0.28)), url("tupi.jpg");
  background-size: cover;
  background-position: center;
  background-attachment: fixed;
  background-repeat: no-repeat;
  color: var(--primary); /* Use primary for body text color */
  min-height: 100vh;
  display: flex;
  width: 100vw;
  overflow-x: hidden;
}

.chat-container {
  flex: 1;
  margin: 0;
  /* Match dark-mode visual configuration in light mode as requested */
  background: rgba(16,18,20,0.75);
  backdrop-filter: blur(6px) saturate(110%);
  border-radius: 0;
  box-shadow: none;
  padding: 20px;
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: none;
  height: 100vh;
}

.chat-header {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 0px;
  padding-bottom: 0px;
  margin-top: 0px;
  padding-top: 0px;
  border-bottom: 1px solid var(--border-color); /* Use adaptive border */
  flex-shrink: 0;
}

.omniakashica-logo {
  width: 32px;
  height: 32px;
  object-fit: contain;
}

.messages {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 6px;
  flex: 1;
  overflow-y: auto;
  padding: 6px;
}

.message {
  display: flex;
  gap: 12px;
  animation: fadeIn 0.3s ease-out;
}

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

.avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-weight: bold;
  flex-shrink: 0;
}

.message-content {
  background: var(--light); /* Use light background variable (which adapts) */
  padding: 12px 16px;
  border-radius: 12px;
  max-width: 70%;
  overflow-wrap: break-word;
  position: relative;
}

.message.user .message-content {
  background: var(--accent);
  color: white;
}

.copy-message-btn {
  position: absolute;
  top: 4px;
  right: 4px;
  background: none;
  border: none;
  font-size: 16px;
  cursor: pointer;
  opacity: 0.6;
  padding: 0px;
  transition: opacity 0.2s;
}

.copy-message-btn:hover {
  opacity: 1;
}

.message.user .message-content .copy-message-btn {
  display: block;
  color: white;
  opacity: 0.7;
}

.message.user .message-content .copy-message-btn:hover {
  opacity: 1;
}

.input-container {
  display: flex;
  gap: 12px;
  margin-top: auto;
  flex-shrink: 0;
}

/* New wrapper for the integrated input and button */
.input-wrapper {
    position: relative;
    flex: 1; /* Ensures the wrapper takes the available width */
}

.model-select {
  flex: 2;
  padding: 5px;
  margin-left: 10px;
}

textarea {
  flex: 1;
  padding: 12px 60px 30px 12px; /* Adjusted padding: Right (60px) and Bottom (30px) for button clearance */
  border: 2px solid var(--border-color); /* Use adaptive border */
  background: var(--bg-container); /* Adaptive background */
  color: var(--primary); /* Adaptive text color */
  border-radius: 8px;
  resize: none;
  font-family: inherit;
  font-size: 1rem;
  min-height: 60px;
  width: 100%; /* Ensure full width relative to wrapper */
  box-sizing: border-box;
}

button {
  padding: 9px 9px;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 7px;
  cursor: pointer;
  font-weight: bold;
  transition: opacity 0.2s;
}

/* Style for the integrated send button */
.send-button {
    position: absolute;
    bottom: 8px; 
    right: 8px; 
    padding: 6px 12px; 
    z-index: 10;
}

button:hover {
  opacity: 0.9;
}

.api-key-button {
  background: var(--accent);
  border: none;
  border-radius: 50%;
  width: 32px;
  height: 32px;
  padding: 6px;
  cursor: pointer;
  margin-left: 8px;
  font-size: 16px;
}

.modal {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,0.5);
  z-index: 1001;
  justify-content: center;
  align-items: center;
}

.modal.active {
  display: flex;
}

.modal-content {
  background: var(--bg-container); /* Adaptive background */
  padding: 24px;
  border-radius: 12px;
  width: 90%;
  max-width: 400px;
  position: relative;
  color: var(--primary); /* Adaptive text color */
}

.close-modal {
  position: absolute;
  top: 12px;
  right: 12px;
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
  color: var(--secondary);
}

.api-key-input {
  width: flex;
  padding: 8px;
  margin-top: 8px;
  border: 1px solid var(--secondary);
  border-radius: 4px;
  margin-bottom: 20px;
  background: var(--bg-container); /* Adaptive background */
  color: var(--primary); /* Adaptive text color */
}

.modal-buttons {
  display: flex;
  justify-content: center; /* Center the buttons */
  gap: 10px; /* Space between buttons */
}

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

.typing-indicator {
  display: flex;
  gap: 4px;
  padding: 12px;
  background: var(--light); /* Adaptive background */
  border-radius: 12px;
  width: fit-content;
}

.dot {
  width: 8px;
  height: 8px;
  background: var(--secondary);
  border-radius: 50%;
  animation: bounce 1.4s infinite;
}

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

@keyframes bounce {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-4px); }
}

.floating-keyboard-btn {
  position: fixed;
  top: calc(50vh - 170px); /* New position: integrated into stack, centered */
  left: 20px; /* Moved to left */
  right: unset; /* Ensure right is unset */
  width: 40px; /* Reduced size */
  height: 40px; /* Reduced size */
  background: var(--accent);
  color: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  box-shadow: 0 4px 6px rgba(0,0,0,0.1);
  cursor: pointer;
  z-index: 1000;
  transition: transform 0.2s, background 0.2s;
  opacity: 0.7;
}

.floating-keyboard-btn:hover {
  transform: scale(1.1);
  background: var(--primary);
  opacity: 1.0;
}

.input-popup {
  display: none;
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  background: var(--bg-container); /* Adaptive background */
  box-shadow: 0 -4px 6px rgba(0,0,0,0.1);
  z-index: 1001;
  padding: 2px;
  animation: slideUp 0.3s ease-out;
}

.input-popup.active {
  display: block;
}

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

.floating-settings-btn {
  position: fixed;
  top: calc(50vh - 120px);  /* New position: 170 - 50 = 120 */
  left: 20px; /* Moved to left */
  right: unset;
  width: 40px; /* Reduced size */
  height: 40px; /* Reduced size */
  background: var(--secondary);
  color: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  box-shadow: 0 4px 6px rgba(0,0,0,0.1);
  cursor: pointer;
  z-index: 1000;
  transition: transform 0.2s, background 0.2s;
  opacity: 0.7;
}

.floating-settings-btn:hover {
  transform: scale(1.1);
  background: var(--primary);
  opacity: 1.0;
}

.settings-container {
  max-width: 500px;
  margin: 0 auto;
}

.settings-option {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 15px;
}

.settings-option label {
  flex: 1;
}

.settings-option input[type="range"] {
  flex: 2;
}

.settings-option input[type="number"] {
  width: 80px;
  padding: 5px;
  background: var(--bg-container); /* Adaptive background for input */
  color: var(--primary); /* Adaptive text color for input */
  border: 1px solid var(--border-color);
}

.floating-clear-history-btn {
  position: fixed;
  top: calc(50vh - 70px);  /* New position: 120 - 50 = 70 */
  left: 20px; /* Moved to left */
  right: unset;
  width: 40px; /* Reduced size */
  height: 40px; /* Reduced size */
  background: var(--secondary);
  color: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  box-shadow: 0 4px 6px rgba(0,0,0,0.1);
  cursor: pointer;
  z-index: 1000;
  transition: transform 0.2s, background 0.2s;
  opacity: 0.7;
}

.floating-clear-history-btn:hover {
  transform: scale(1.1);
  background: var(--primary);
  opacity: 1.0;
}

.floating-export-history-btn {
  position: fixed;
  top: calc(50vh - 20px);  /* New position: 70 - 50 = 20 */
  left: 20px; /* Moved to left */
  right: unset;
  width: 40px; /* Reduced size */
  height: 40px; /* Reduced size */
  background: var(--secondary);
  color: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  box-shadow: 0 4px 6px rgba(0,0,0,0.1);
  cursor: pointer;
  z-index: 1000;
  transition: transform 0.2s, background 0.2s;
  opacity: 0.7;
}

.floating-export-history-btn:hover {
  transform: scale(1.1);
  background: var(--primary);
  opacity: 1.0;
}

/* New Home button */
.floating-home-btn {
  position: fixed;
  top: calc(50vh + 30px); /* New position: -20 + 50 = 30 */
  left: 20px; /* Moved to left */
  right: unset;
  width: 40px; /* Reduced size */
  height: 40px; /* Reduced size */
  background: var(--secondary);
  color: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  box-shadow: 0 4px 6px rgba(0,0,0,0.1);
  cursor: pointer;
  z-index: 1000;
  transition: transform 0.2s, background 0.2s;
  opacity: 0.7;
}

.floating-home-btn:hover {
  transform: scale(1.1);
  background: var(--primary);
  opacity: 1.0;
}

.floating-api-key-btn {
  position: fixed;
  top: calc(50vh + 80px);  /* New position: 30 + 50 = 80 */
  left: 20px; /* Moved to left */
  right: unset;
  width: 40px; /* Reduced size */
  height: 40px; /* Reduced size */
  background: var(--secondary);
  color: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  box-shadow: 0 4px 6px rgba(0,0,0,0.1);
  cursor: pointer;
  z-index: 1000;
  transition: transform 0.2s, background 0.2s;
  opacity: 0.7;
}

.floating-api-key-btn:hover {
  transform: scale(1.1);
  background: var(--primary);
  opacity: 1.0;
}

/* NEW Dark Mode button */
.floating-dark-mode-btn {
  position: fixed;
  top: calc(50vh + 130px);  /* New position: 80 + 50 = 130 */
  left: 20px; /* Moved to left */
  right: unset;
  width: 40px; /* Reduced size */
  height: 40px; /* Reduced size */
  background: var(--secondary);
  color: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  box-shadow: 0 4px 6px rgba(0,0,0,0.1);
  cursor: pointer;
  z-index: 1000;
  transition: transform 0.2s, background 0.2s;
  opacity: 0.7;
}

.floating-dark-mode-btn:hover {
  transform: scale(1.1);
  background: var(--primary);
  opacity: 1.0;
}

/* Compact override: reduce size of all floating action buttons */
.floating-keyboard-btn,
.floating-settings-btn,
.floating-clear-history-btn,
.floating-export-history-btn,
.floating-home-btn,
.floating-api-key-btn,
.floating-dark-mode-btn {
  width: 32px;
  height: 32px;
  font-size: 16px;
}

@media (max-width: 600px) {
  body {
    font-size: 14px; /* Slightly smaller base font size for mobile */
  }

  .chat-container {
    padding: 10px; /* Reduce padding on smaller screens */
  }

  .chat-header {
    flex-direction: row; /* Revert to row for better compact layout */
    justify-content: space-between; /* Spread out items */
    align-items: center;
  }

  .chat-header h1 {
    font-size: 1.2rem; /* Smaller header text */
    margin: 0;
  }

  .chat-header h3 {
    font-size: 1.2rem; /* Smaller header text */
    margin: 0;
  }

  .messages {
    padding: 5px; /* Reduce padding */
    gap: 10px; /* Tighter message spacing */
  }

  .message-content {
    max-width: 85%; /* Allow slightly wider messages */
    font-size: 0.9rem; /* Slightly smaller message text */
  }

  .input-container {
    flex-direction: row; /* Keep input and button in one row */
    align-items: stretch; /* Stretch items to same height */
  }

  textarea {
    flex: 1;
    min-height: 50px; /* Slightly shorter input */
    padding: 8px; /* Reduce padding */
  }

  button {
    padding: 8px; /* Slightly smaller buttons */
    font-size: 0.9rem;
  }

  .modal-content {
    width: 95%;
    max-width: 95%;
    margin: 0 10px;
    padding: 15px; /* Reduce modal padding */
  }

  .modal-buttons {
    flex-direction: column; /* Stack modal buttons vertically on mobile */
    gap: 8px;
  }

  .input-container {
    flex-direction: column;
  }

  .input-popup .input-container {
    gap: 10px;
  }

  .input-popup textarea {
    width: 100%;
    min-height: 100px;
    padding: 12px 60px 30px 12px; /* Ensure clearance is maintained on mobile */
  }

  .input-popup button {
    width: 100%;
  }
  
  /* Ensure the absolutely positioned button retains its natural size on mobile */
  .input-popup .send-button {
    width: auto;
  }

  .model-select {
    width: 100%;
    margin-left: 0;
  }

  .settings-option {
    flex-direction: column;
    align-items: flex-start;
  }
  
  .settings-option label {
    width: 100%;
  }

  .settings-option input[type="range"],
  .settings-option input[type="number"] {
    width: 100%;
  }
  
  /* Ensure save settings button is full width on mobile */
  .settings-container button {
    width: 100%;
    margin-top: 10px;
  }
  
  .floating-keyboard-btn,
  .floating-settings-btn,
  .floating-clear-history-btn,
  .floating-export-history-btn,
  .floating-home-btn, /* New: Add mobile styling for home button */
  .floating-api-key-btn,
  .floating-dark-mode-btn {
    width: 32px; /* Reduced size for mobile */
    height: 32px; /* Reduced size for mobile */
    font-size: 18px; /* Adjusted font size */
    right: unset; /* Remove right constraint */
    left: 10px; /* Move closer to edge on mobile */
  }
  
  /* Reposition floating buttons to center vertically on mobile */
  /* Using 32px size and 42px center-to-center distance */
  
  .floating-keyboard-btn {
    top: calc(50vh - 142px);
    bottom: unset;
  }
  
  .floating-settings-btn {
    top: calc(50vh - 100px);
    bottom: unset;
  }

  .floating-clear-history-btn {
    top: calc(50vh - 58px);
    bottom: unset;
  }

  .floating-export-history-btn {
    top: calc(50vh - 16px);
    bottom: unset;
  }

  .floating-home-btn {
    top: calc(50vh + 26px);
    bottom: unset;
  }

  .floating-api-key-btn {
    top: calc(50vh + 68px); 
    bottom: unset;
  }
  
  /* NEW Dark Mode button mobile position */
  .floating-dark-mode-btn {
    top: calc(50vh + 110px); 
    bottom: unset;
  }
}

@media (max-width: 900px) {
  /* mobile / small tablet adjustments */
  .chat-container {
    padding: 12px;
    height: 100vh;
    box-sizing: border-box;
  }

  .messages { gap: 10px; padding: 8px; }

  textarea {
    padding: 12px 72px 40px 12px; /* extra right padding for large touch send button */
    min-height: 84px;
    font-size: 1rem;
  }

  .input-popup .input-container { padding: 8px 12px; }

  /* Make floating buttons slightly larger for touch targets and align left stacked centered vertically */
  .floating-keyboard-btn,
  .floating-settings-btn,
  .floating-clear-history-btn,
  .floating-export-history-btn,
  .floating-home-btn,
  .floating-api-key-btn,
  .floating-dark-mode-btn {
    width: 44px;
    height: 44px;
    font-size: 20px;
    left: 12px;
    box-shadow: 0 6px 10px rgba(0,0,0,0.12);
  }

  /* Compact vertical spacing using consistent step */
  .floating-keyboard-btn { top: calc(50vh - 132px); }
  .floating-settings-btn { top: calc(50vh - 88px); }
  .floating-clear-history-btn { top: calc(50vh - 44px); }
  .floating-export-history-btn { top: calc(50vh - 0px); }
  .floating-home-btn { top: calc(50vh + 44px); }
  .floating-api-key-btn { top: calc(50vh + 88px); }
  .floating-dark-mode-btn { top: calc(50vh + 132px); }
}

@media (max-width: 420px) {
  /* Very small screens: full-width send button inside input bar, larger tap target */
  .input-wrapper { position: relative; }
  .send-button {
    position: absolute;
    bottom: 8px;
    right: 8px;
    padding: 10px 14px;
    border-radius: 10px;
    font-size: 16px;
    touch-action: manipulation;
  }

  .input-popup .send-button {
    width: 52px;
    height: 44px;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  /* Ensure messages have comfortable line-height and spacing */
  .message-content { font-size: 15px; line-height: 1.4; padding: 12px 16px; }
  .avatar { width: 36px; height: 36px; font-size: 14px; }

  /* Keep modals full-screen friendly */
  .modal-content { width: 94%; max-width: 420px; padding: 16px; }
}

@media (max-height: 600px) {
  .chat-container {
    height: 100vh;
    overflow-y: auto; /* Allow scrolling if content is too tall */
  }
}