/* ==========================================================================
   1. RESET & BASE
   ========================================================================== */
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  width: 100%;
  min-height: 100dvh;
  overflow-x: hidden;
  font-family: sans-serif;
  position: relative;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.3s ease, color 0.3s ease;
  background: var(--bg-primary);
  color: var(--text-primary);
}

/* ==========================================================================
   2. VARIABLES (THEMING)
   ========================================================================== */

/* Dark Mode (Default) */
:root {
  --bg-primary: linear-gradient(to bottom, #05100a, #0a2b17);
  --bg-secondary: radial-gradient(circle at top right, #0a2b17, #05100a);
  --bg-overlay: rgba(0,0,0,0.3);
  --bg-overlay-light: rgba(255,255,255,0.1);
  --bg-overlay-hover: rgba(255,255,255,0.2);
  
  --text-primary: #8d8989;
  --text-secondary: #2b2b2b;
  --text-bubble-outgoing: #111111;
  
  --bubble-bg-incoming: #f5f5f5;
  --bubble-bg-outgoing: linear-gradient(135deg, #ff6ec7, #7873f5);
  --bubble-arrow-incoming: #f5f5f5;
  --bubble-arrow-outgoing: #7873f5;
  
  --button-bg: #A3E635;
  --button-bg-danger: #ff4d4d;
  --button-hover: #C6F6D5;
  --button-hover-danger: #ff6666;
  
  --link-color: #A3E635;
  --link-hover: #C6F6D5;
  --shadow: transparent;
  --input-bg: #f5f5f5;
  
  --chip-bg: #4CAF50;
  --chip-hover: #45a049;
  
  --border-color: rgba(255,255,255,0.2);
  --name-color-incoming: #3498db;
  --name-color-outgoing: #116934;
  --timestamp-color: #444444;
  
  --history-bg: rgba(0,0,0,0.5);
  --history-text: rgba(255,255,255,0.7);
}

/* Light Mode Overrides */
body.light-mode {
  --bg-primary: linear-gradient(to bottom, #aaaaaa, #ffffff);
  --bg-secondary: radial-gradient(circle at top right, #ffffff, #aaaaaa);
  --bg-overlay: rgba(255,255,255,0.3);
  --bg-overlay-light: rgba(0,0,0,0.1);
  --bg-overlay-hover: rgba(0,0,0,0.2);
  
  --text-primary: #333333;
  --text-secondary: rgba(0,0,0,0.7);
  
  --bubble-bg-incoming: #e0e0e0;
  --bubble-bg-outgoing: linear-gradient(135deg, #ff9ed7, #a0a0ff);
  --bubble-arrow-incoming: #e0e0e0;
  --bubble-arrow-outgoing: #a0a0ff;
  
  --button-bg: #4CAF50;
  --button-bg-danger: #d32f2f;
  --button-hover: #66BB6A;
  --button-hover-danger: #e57373;
  
  --link-color: #4CAF50;
  --link-hover: #66BB6A;
  --shadow: rgba(0,0,0,0.1);
  --input-bg: #ffffff;
  
  --chip-bg: #A3E635;
  --chip-hover: #C6F6D5;
  
  --border-color: rgba(0,0,0,0.2);
  --name-color-incoming: #1976d2;
  --name-color-outgoing: #0a401d;
  --timestamp-color: #222222;
  --text-bubble-outgoing: #000000;
  
  --history-bg: rgba(255,255,255,0.5);
  --history-text: rgba(0,0,0,0.7);
}

/* ==========================================================================
   3. UTILITY COMPONENTS & BUTTONS
   ========================================================================== */

/* Button System */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 999px; /* Pill shape */
  padding: clamp(8px, 2vw, 12px) clamp(16px, 4vw, 24px);
  font-size: clamp(14px, 3vw, 16px);
  cursor: pointer;
  transition: background 0.3s ease, transform 0.2s ease;
  border: none;
  color: var(--text-secondary);
}

.btn:hover {
  transform: scale(1.05);
  background: var(--button-hover);
}

.btn-primary { background: var(--button-bg); }
.btn-secondary { background: transparent; border: 1px solid var(--button-bg); color: var(--button-bg); }
.btn-danger { background: var(--button-bg-danger); }
.btn-danger:hover { background: var(--button-hover-danger); }

/* Icon Buttons */
.btn-icon {
  padding: 0;
  min-width: 48px;
  min-height: 48px;
  border-radius: 50%;
}

.btn-icon img, .menu-icon, .tts-icon, .theme-icon {
  width: 50%;
  height: auto;
  max-width: 24px;
  max-height: 24px;
  object-fit: contain;
  display: block;
  margin: 0 auto;
}

.btn-icon:hover img { transform: scale(1.1); }

/* Toggle Switch */
.switch {
  position: relative;
  display: inline-block;
  width: 51px;
  height: 31px;
}
.switch input { opacity: 0; width: 0; height: 0; }
.slider {
  position: absolute;
  cursor: pointer;
  top: 0; left: 0; right: 0; bottom: 0;
  background-color: #ccc;
  transition: .4s;
}
.slider:before {
  position: absolute;
  content: "";
  height: 23px; width: 23px;
  left: 4px; bottom: 4px;
  background-color: white;
  transition: .4s;
}
input:checked + .slider { background-color: var(--button-bg); }
input:focus + .slider { box-shadow: 0 0 1px var(--button-bg); }
input:checked + .slider:before { transform: translateX(20px); }
.slider.round { border-radius: 34px; }
.slider.round:before { border-radius: 50%; }

/* ==========================================================================
   4. LAYOUT: LANDING PAGE
   ========================================================================== */
#landing {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  min-height: 100dvh;
  z-index: 1;
  text-align: center;
  padding: 1rem;
  background: var(--bg-secondary);
}
#starfield {
  position: fixed;
  top: 0; left: 0;
  width: 100%; height: 100dvh;
  z-index: 0;
  pointer-events: none;
  display: none;
}
#landing #starfield { display: block; }

.landing-container {
  width: 100%;
  max-width: 600px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1.5rem;
}
.top-links {
  display: flex;
  gap: 0.75rem;
  width: 100%;
}
.link-block {
  flex: 1;
  padding: 0.75rem;
  background: var(--bg-overlay-light);
  border-radius: 8px;
  color: var(--text-primary);
  text-decoration: none;
  font-weight: bold;
  transition: background .2s;
}
.link-block:hover { background: var(--bg-overlay-hover); }

.bot-placeholder img {
  width: clamp(100px, 25vw, 150px);
  border-radius: 50%;
  box-shadow: 0 0 20px var(--shadow);
}

.recent-chats {
  background: var(--bg-overlay);
  padding: 1rem;
  border-radius: 8px;
  width: 100%;
}
.recent-chats .recent-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.5rem;
}
.recent-chats .recent-header h2 {
  margin: 0;
  font-size: clamp(1.2rem, 4vw, 1.5rem);
  color: var(--text-primary);
}
.recent-chats .see-all {
  text-decoration: none;
  font-size: 0.9rem;
  color: var(--link-color);
  transition: color 0.2s;
}
.recent-chats .see-all:hover { color: var(--link-hover); }

#recentList { list-style: none; padding: 0; margin: 0; }
#recentList li {
  padding: 0.5rem 0;
  border-bottom: 1px solid var(--border-color);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: clamp(0.85rem, 2.5vw, 0.95rem);
  cursor: pointer;
  color: var(--text-primary);
}
#recentList li:hover { background: var(--bg-overlay-hover); }

#startChat {
  font-size: clamp(1rem, 4vw, 1.25rem);
  padding: 0.75rem 1.5rem;
  background: var(--bg-overlay-light);
  border: 2px solid var(--border-color);
  border-radius: 40px;
  cursor: pointer;
  transition: background .2s, transform .2s;
  color: var(--text-primary);
}
#startChat:hover {
  background: var(--bg-overlay-hover);
  transform: scale(1.05);
}

/* ==========================================================================
   5. LAYOUT: CHAT APPLICATION
   ========================================================================== */
#chatApp {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  display: flex;
  flex-direction: column;
  z-index: 1;
}

/* Header */
.chatBot header.chat-header {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  padding: 0.5rem 1rem;
  padding-top: env(safe-area-inset-top, 10px);
  background: var(--bg-primary);
  position: sticky;
  top: 0;
  width: 100%;
  z-index: 1000;
  gap: 0.5rem;
  box-shadow: 0 2px 10px var(--shadow);
}
.header-title {
  flex: 1;
  text-align: center;
  font-size: clamp(1rem, 3vw, 1.2rem);
  margin: 0;
  color: var(--text-primary);
}

/* Dropdown Menu */
.header-menu {
  position: absolute;
  top: 8%; right: 0;
  min-width: 180px; max-width: 80vw;
  background: var(--bg-overlay);
  border-radius: 16px;
  box-shadow: var(--shadow);
  z-index: 1;
  transform: translateY(-10px);
  transition: transform 0.3s ease-in-out, opacity 0.3s ease-in-out;
  opacity: 0;
  pointer-events: none;
  display: none;
}
.header-menu.open {
  display: block;
  transform: translateY(0);
  opacity: 1;
  pointer-events: auto;
}
.menu-list {
  list-style: none;
  padding: 0.5rem;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}
.menu-item {
  width: 100%;
  justify-content: flex-start;
  gap: 0.5rem;
  white-space: normal;
  color: #000;
  background-color: #2ecc71;
}
.menu-item .menu-icon {
  width: 20px; height: 20px;
  margin-right: 0.5rem;
}

/* Chat Area */
.chatBot {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  position: relative;
}
.chatbox {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: clamp(60px, 5vw, 80px) clamp(15px, 2vw, 20px);
  list-style: none;
  max-height: calc(100vh - 120px);
  min-height: 200px;
  box-sizing: border-box;
  display: block;
}

/* Input Area */
.chat-input {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  display: flex; /* Initially hidden via JS, but styled as flex */
  align-items: center;
  padding: clamp(0.5rem, 2vw, 1rem);
  padding-bottom: env(safe-area-inset-bottom, 0.5rem);
  background: var(--bg-overlay);
  box-shadow: 0 -2px 10px var(--shadow);
}
#chatInputContainer { display: none; }

.chat-input textarea {
  flex: 1;
  width: 100%;
  min-height: 2rem;
  max-height: 8rem;
  margin-right: 0.15rem;
  padding: 0.6rem 1rem;
  border-radius: 20px;
  border: none;
  background: var(--input-bg);
  color: #000000;
  font-size: clamp(0.9rem, 2.5vw, 1.1rem);
  resize: vertical;
  transition: min-height 0.2s ease;
}
.chat-input textarea:focus {
  outline: none;
  box-shadow: 0 0 5px rgba(163, 230, 53, 0.5);
  min-height: 2.5rem;
}

.send-mic-wrapper {
  display: flex;
  gap: 0;
  margin-left: 0;
  flex: 0 0 10%;
}
.send-button, .mic-button, .theme-toggle, .menu-trigger {
  padding: clamp(0.5rem, 2vw, 0.8rem);
  min-width: 48px;
  min-height: 48px;
  margin-right: 0.15rem;
  border: none;
  background: var(--button-bg);
  color: var(--text-secondary);
  border-radius: 20px;
  cursor: pointer;
  transition: background 0.2s, transform 0.1s;
  border-left: 1px solid var(--border-color);
}
.send-button:hover, .mic-button:hover, .theme-toggle:hover {
  background: var(--button-hover);
  transform: scale(1.05);
}
.send-button img, .mic-button img, .menu-trigger img {
  width: 80%;
  height: auto;
  display: block;
  margin: 0 auto;
  object-fit: contain;
}

/* ==========================================================================
   6. COMPONENT: CHAT BUBBLES (FIXED)
   ========================================================================== */
.chat {
  /* CHANGE: Increased from 0.5rem to 1.5rem for better spacing */
  margin: 1.5rem 0; 
  
  width: 100%;
  list-style: none;
}


.chat-bubble {
  display: flex;
  align-items: flex-start;
  gap: 0.5rem;
  width: 100%;
}

/* Direction modifiers applied to the wrapper */
.chat-incoming .chat-bubble { flex-direction: row; }
.chat-outgoing .chat-bubble { flex-direction: row-reverse; }

.chat-image {
  width: clamp(40px, 6vw, 50px);
  height: clamp(40px, 6vw, 50px);
  border-radius: 50%;
  object-fit: cover;
  flex-shrink: 0;
}

.speech-wrapper {
  display: flex;
  flex-direction: column;
  position: relative;
  flex-grow: 1;
  min-width: 0;
}
.chat-incoming .speech-wrapper { max-width: 90%; }
.chat-outgoing .speech-wrapper { max-width: 85%; }

.bubble {
  max-width: 100%;
  position: relative;
  padding: 0.8rem 1rem; /* Slightly more padding for better look */
  border-radius: 8px;
  box-shadow: 0 2px 5px var(--shadow);
  background: var(--bubble-bg-incoming);
}

.bubble.alt {
  margin-left: auto;
  background: var(--bubble-bg-outgoing);
  color: var(--text-bubble-outgoing);
}

.bubble-arrow {
  position: absolute;
  width: 0; height: 0;
  top: 12px; /* Adjusted to align slightly better with the top line of text */
  border: 8px solid transparent; /* This creates the arrow shape */
  pointer-events: none; /* Ensures the arrow doesn't block clicks */
}

/* Incoming Arrow: Points Left */
.chat-incoming .bubble-arrow {
  /* CHANGE: Set to -8px (same as border size) so it touches the bubble */
  left: -8px; 
  border-right-color: var(--bubble-arrow-incoming);
  border-left: 0;
}

/* Outgoing Arrow: Points Right */
.chat-outgoing .bubble-arrow {
  /* CHANGE: Set to -8px (same as border size) so it touches the bubble */
  right: -8px; 
  border-left-color: var(--bubble-arrow-outgoing);
  border-right: 0;
}

/* Text Styling */
.chat .txt { position: relative; }

.name {
  font-weight: 600;
  font-size: clamp(10px, 2vw, 12px);
  margin-bottom: 4px;
  color: var(--name-color-incoming);
}
.name.alt { color: var(--name-color-outgoing); }
.name span {
  font-size: 0.75em;
  color: var(--timestamp-color);
  font-weight: normal;
  margin-left: 0.5rem;
}

.message {
  font-size: clamp(1rem, 3vw, 1.2rem);
  line-height: 1.5;
  margin: 0;
  color: var(--text-secondary);
  word-wrap: break-word; /* Prevent overflow */
}
.bubble.alt .message { color: var(--text-bubble-outgoing); }

/* Chips inside Chat */
.chip {
  display: inline-block;
  padding: clamp(6px, 1.5vw, 8px) clamp(12px, 3vw, 16px);
  margin: 4px;
  background-color: var(--chip-bg);
  color: var(--text-secondary);
  border-radius: 16px;
  cursor: pointer;
  font-size: clamp(12px, 2.5vw, 14px);
  transition: background-color 0.3s;
}
.chip:hover { background-color: var(--chip-hover); }

/* Markdown Formatting */
.bubble .message p { margin-bottom: 0.5rem; }
.bubble .message ul, .bubble .message ol { margin-left: 1.5rem; margin-bottom: 0.5rem; }
.bubble .message li { list-style-position: outside; }
.bubble .message strong { font-weight: bold; }
.bubble .message em { font-style: italic; }
.bubble .message code {
  background: rgba(0,0,0,0.2);
  padding: 2px 4px;
  border-radius: 4px;
  font-family: monospace;
}
.bubble .message pre {
  background: rgba(0,0,0,0.3);
  padding: 10px;
  border-radius: 8px;
  overflow-x: auto;
  margin-bottom: 0.5rem;
}

/* ==========================================================================
   7. COMPONENT: SETTINGS MODAL
   ========================================================================== */
.settings-modal-container {
  position: fixed;
  top: 0; left: 0;
  width: 100%; height: 100dvh;
  background: var(--bg-primary);
  z-index: 2000;
  display: flex;
  flex-direction: column;
  transform: translateY(100%);
  transition: transform 0.3s ease-in-out;
  color: var(--text-primary);
}
.settings-modal-container.open { transform: translateY(0%); }

.settings-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem;
  padding-top: env(safe-area-inset-top, 1rem);
  background: var(--bg-overlay);
  flex-shrink: 0;
}
.settings-header h1 { font-size: 1.5rem; margin: 0; }

.settings-content {
  flex-grow: 1;
  overflow-y: auto;
  padding: 1rem;
  padding-bottom: env(safe-area-inset-bottom, 1rem);
}
.settings-group {
  margin-bottom: 2rem;
  background: var(--bg-overlay-light);
  border-radius: 8px;
  padding: 1rem;
}
.settings-group-title {
  font-size: 1rem;
  font-weight: bold;
  text-transform: uppercase;
  color: var(--text-secondary);
  margin-bottom: 1.5rem;
  border-bottom: 1px solid var(--border-color);
  padding-bottom: 0.5rem;
}

.settings-item, .settings-item-toggle {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
}
.settings-item label { font-size: 1.1rem; color: var(--text-primary); }

.settings-select {
  padding: 0.5rem;
  border-radius: 4px;
  border: 1px solid var(--border-color);
  background: var(--input-bg);
  color: var(--text-primary);
  min-width: 150px;
  width: 50%;
  font-family: inherit;
  font-size: 1rem;
}
.settings-select:focus {
  outline: none;
  border-color: var(--button-bg);
  box-shadow: 0 0 3px var(--button-bg);
}

.settings-item-button { margin-bottom: 1rem; }
.settings-item-button .btn { width: 100%; }

/* ==========================================================================
   8. COMPONENT: HISTORY PAGE
   ========================================================================== */
.history-container {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: 600px;
  margin: 0 auto;
  padding: 1rem;
  background: var(--history-bg);
  border-radius: 8px;
  min-height: 100dvh;
  padding-bottom: env(safe-area-inset-bottom, 1rem);
}
.history-container header {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-bottom: 1rem;
}
.history-container .nav-buttons {
  display: flex;
  gap: 1rem;
  margin-left: auto;
}
.back-button, .clear-history-btn, .landing-button {
  background: var(--button-bg);
  color: var(--text-secondary);
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
  font-size: clamp(12px, 2vw, 14px);
}
.clear-history-btn { background: var(--button-bg-danger); }
.back-button:hover, .landing-button:hover { background: var(--button-hover); }
.clear-history-btn:hover { background: var(--button-hover-danger); }

.history-list {
  max-height: calc(100dvh - 6rem - env(safe-area-inset-bottom, 0));
  overflow-y: auto;
  padding-right: 0.5rem;
}
.date-separator {
  margin: 1rem 0 0.5rem;
  font-weight: bold;
  color: var(--text-primary);
  text-align: center;
}

/* History Message Styling */
.history-message {
  margin: 0.5rem 0;
  padding: 0.75rem;
  border-radius: 8px;
  max-width: 85%;
  font-size: clamp(0.8rem, 2vw, 0.9rem);
}
.history-message.incoming {
  background: var(--bg-overlay-light);
  align-self: flex-start;
}
.history-message.outgoing {
  background: var(--bg-overlay);
  align-self: flex-end;
}
.history-timestamp {
  display: block;
  font-size: 0.75rem;
  text-align: right;
  margin-top: 0.5rem;
  opacity: 0.7;
  color: inherit;
}
.history-settings {
    display: flex;
    gap: 0.5rem;
    justify-content: center;
    margin: 1rem 0;
}

/* Filter Chips */
.filter-chip-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  padding: 0 1rem 1rem 1rem;
  border-bottom: 1px solid var(--border-color);
  margin-bottom: 1rem;
}
.filter-chip-btn {
  background: var(--bg-overlay-light);
  border: 1px solid var(--border-color);
  color: var(--text-primary);
  padding: 6px 16px;
  border-radius: 20px;
  cursor: pointer;
  font-size: clamp(0.8rem, 2vw, 0.9rem);
  transition: all 0.2s ease;
}
.filter-chip-btn:hover { background: var(--bg-overlay-hover); transform: translateY(-1px); }
.filter-chip-btn.active {
  background: var(--button-bg);
  color: var(--text-secondary);
  border-color: var(--button-bg);
  font-weight: 600;
  box-shadow: 0 2px 5px var(--shadow);
}

.load-history-trigger {
  display: flex;
  justify-content: center;
  width: 100%;
  list-style: none;
  margin-bottom: 1rem;
}
.load-history-trigger .btn-small {
  background: var(--bg-overlay-light);
  color: var(--text-primary);
  border: 1px solid var(--border-color);
  width: auto !important;
  max-width: 250px;
}
.load-history-trigger .btn-small:hover { background: var(--bg-overlay-hover); }

/* ==========================================================================
   9. ANIMATIONS & LOADERS
   ========================================================================== */
#micButton.listening {
  opacity: 0.7;
  animation: pulse 1s infinite;
}
@keyframes pulse {
  0%,100% { transform: scale(1); }
  50% { transform: scale(1.1); }
}

.typing-loader {
  color: var(--text-secondary);
  width: fit-content;
  font-weight: bold;
  font-family: inherit;
  font-size: clamp(20px, 4vw, 30px);
  clip-path: inset(0 100% 0 0);
  animation: l5 3s steps(11) infinite;
  margin: 0;
}
.typing-loader:before { content: "Thinking..."; }
@keyframes l5 { to { clip-path: inset(0 0 0 0); } }

/* ==========================================================================
   10. MEDIA QUERIES (RESPONSIVE)
   ========================================================================== */
@media (max-width: 768px) {
  .chat-bubble { margin-bottom: 0.8rem; }
  .chat-image { width: 35px; height: 35px; }
  .speech-wrapper { padding: 0.4rem; }
  .chat-input textarea { min-height: 1.8rem; }
}

@media (max-width: 480px) {
  /* Buttons */
  .send-button, .mic-button, .theme-toggle {
    padding: clamp(0.4rem, 1.5vw, 0.6rem);
    min-width: 40px; min-height: 40px;
  }
  .btn-icon { min-width: 40px; min-height: 40px; }
  
  /* Images */
  .btn-icon img, .menu-icon, .tts-icon, .theme-icon { width: 60%; }
  .menu-item .menu-icon { width: 18px; height: 18px; }
  .chat-image {
    width: 32px; height: 32px;
    margin-top: 0.3rem;
  }
  
  /* Layout */
  .speech-wrapper { padding: 0.3rem; }
  .bubble { max-width: 90%; }
  .chat-input textarea { min-height: 1.6rem; padding: 0.5rem 0.8rem; }
  #startChat { width: 100%; }
  .header-title { font-size: clamp(1rem, 3vw, 1.2rem); }
}

@media (max-width: 360px) {
  #startChat { width: 100%; }
}