/* Foundational Layout Rules (Debug Borders Removed) */
html, body {
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
    overflow-x: hidden;
    box-sizing: border-box;
}

*, *::before, *::after {
    box-sizing: inherit;
}

.app-wrapper {
    display: flex;
    width: 100%;
    height: 100%;
    max-width: 100%; /* Ensure it uses full width initially */
    margin: 0 auto;
}

.chat-list-sidebar {
    width: 260px; /* Default width */
    min-width: 200px; /* Minimum width before it gets too squeezed */
    height: 100%;
    flex-shrink: 0;
    overflow-y: auto;
    transition: margin-left 0.3s ease-in-out; /* For mobile toggle */
}

.app-main-content { 
    flex-grow: 1; 
    height: 100%;
    display: flex;
    flex-direction: column;
    overflow: hidden; 
}

/* --- Main UI Overhaul Styles --- */
:root {
  --font-family-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
  --font-family-mono: SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;

  --primary-color: #007AFF;
  --primary-color_01: rgba(0,122,255,0.1);
  --primary-color_03: rgba(0,122,255,0.3);
  --primary-color-rgb: 0,122,255;

  --text-color-primary: #1d1d1f;
  --text-color-secondary: #515154;
  --text-color-placeholder: #86868b;
  --background-color-page: #f0f2f5; /* Page background, slightly off-white/grey */
  --background-color-content: #ffffff; /* Chat and sidebar content background */
  --background-color-elements: #f8f9fa; /* Buttons, inputs background */
  
  --border-color-light: #e1e1e4;
  --border-color-medium: #c7c7cc;
  --system-red: #FF3B30;
  --system-green: #34C759;
  --system-blue: var(--primary-color);
  
  --header-height: 56px;
  --footer-height: auto;
  --chat-padding: 16px;
  --message-max-width: 75%;
  --message-padding: 10px 14px;
  --message-border-radius: 18px;
  --input-border-radius: 22px;
  --button-border-radius: 10px;
  --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.04);
  --shadow-md: 0 3px 5px -1px rgba(0, 0, 0, 0.05), 0 2px 3px -1px rgba(0, 0, 0, 0.04);
}

html {
  font-size: 16px; /* Base font size for easier rem calculations if needed */
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: var(--font-family-sans);
  color: var(--text-color-primary);
  background-color: var(--background-color-page); 
  line-height: 1.5;
  overscroll-behavior-y: contain;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* Sidebar specific styles */
.chat-list-sidebar {
  background-color: var(--background-color-elements); /* Distinct from main chat area */
  border-right: 1px solid var(--border-color-light);
}

.sidebar-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 var(--chat-padding);
  height: var(--header-height);
  border-bottom: 1px solid var(--border-color-light);
  flex-shrink: 0;
}

.sidebar-title {
  font-size: 1.05rem; /* Slightly smaller */
  font-weight: 600;
}

#sidebar-new-chat-button svg {
  color: var(--primary-color);
}

.chat-list-ul {
  list-style: none;
  padding: 8px;
  margin: 0;
  overflow-y: auto;
  flex-grow: 1;
}

.chat-list-item {
  padding: 10px 12px;
  border-radius: var(--button-border-radius);
  cursor: pointer;
  margin-bottom: 4px;
  font-size: 0.9rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: background-color 0.15s ease, color 0.15s ease;
  border: 1px solid transparent;
  color: var(--text-color-secondary);
}

.chat-list-item:hover {
  background-color: var(--primary-color_01);
  color: var(--primary-color);
}

.chat-list-item.active {
  background-color: var(--primary-color);
  color: white;
  font-weight: 500;
  border-color: var(--primary-color);
}

.chat-list-item .chat-name {
  flex-grow: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-right: 8px; /* Space between name and delete button */
}

.chat-list-item {
  display: flex; /* To align name and button */
  justify-content: space-between;
  align-items: center;
}

.delete-chat-button {
  background: none;
  border: none;
  color: var(--text-color-secondary);
  padding: 4px;
  margin-left: 4px;
  cursor: pointer;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0; /* Prevent button from shrinking */
}

.delete-chat-button:hover {
  color: var(--system-red);
  background-color: rgba(var(--system-red-rgb, 255, 59, 48), 0.1);
}

.chat-list-item.active .delete-chat-button {
    color: rgba(255,255,255,0.7);
}
.chat-list-item.active .delete-chat-button:hover {
    color: white;
    background-color: rgba(255,255,255,0.2);
}

.chat-list-placeholder {
    padding: 12px;
    text-align: center;
    color: var(--text-color-secondary);
    font-style: italic;
}

.sidebar-toggle {
  display: flex; /* Always display the toggle button */
  font-size: 1.4rem; /* Ensure icon is large enough */
  margin-right: 8px; /* Space from title/next element */
}

/* Main Content Area (.app-main-content) */
.app-main-content {
  background-color: var(--background-color-content);
  /* Removed max-width and margin:auto from here to let app-wrapper control overall width */
  /* box-shadow: var(--shadow-md); Potentially remove if app-wrapper has shadow */
}

.app-header {
  display: flex;
  align-items: center;
  padding: 0 var(--chat-padding);
  height: var(--header-height);
  background-color: var(--background-color-content);
  border-bottom: 1px solid var(--border-color-light);
  flex-shrink: 0;
  justify-content: space-between; /* Distribute space for new layout */
}

.app-title {
  /* font-size: 1.125rem; */
  /* font-weight: 600; */
  /* color: var(--text-color-primary); */
  /* margin-left: auto; */ /* Push other items to the right */
  /* margin-right: auto; */ /* Center title if space allows */
  /* text-align: center; */ 
  /* flex-grow: 1; */ /* Allow title to take space */
}

.header-center-item { /* Class for the model selector wrapper */
  flex-grow: 1;
  display: flex;
  justify-content: center;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  /* margin-left: auto; */ /* No longer needed if space-between on parent */
}

.model-selector-wrapper select {
  padding: 8px 12px; /* Adjusted padding */
  border: none; /* Removed border */
  border-radius: var(--button-border-radius); /* Consistent border radius */
  background-color: var(--background-color-elements);
  font-size: 0.9rem; /* Adjusted font size */
  color: var(--text-color-primary);
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='%23515154' viewBox='0 0 16 16'%3E%3Cpath fill-rule='evenodd' d='M1.646 4.646a.5.5 0 0 1 .708 0L8 10.293l5.646-5.647a.5.5 0 0 1 .708.708l-6 6a.5.5 0 0 1-.708 0l-6-6a.5.5 0 0 1 0-.708z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center; /* Adjusted arrow position */
  padding-right: 34px; /* Ensure space for arrow */
  min-width: 160px; /* Base width */
  text-align: center; /* Center text in select if desired */
  font-weight: 500;
}

.model-selector-wrapper select:focus {
  outline: none;
  box-shadow: 0 0 0 2px rgba(var(--primary-color-rgb), 0.3);
}

.header-button {
  background: none;
  border: none;
  padding: 8px;
  cursor: pointer;
  color: var(--text-color-secondary); /* Default icon color */
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
}
.header-button:hover {
  background-color: var(--background-color-elements);
  color: var(--primary-color);
}
#clear-chat-button svg { color: var(--system-red); }
#api-settings svg { color: var(--primary-color); }

.chat-main-area {
  flex: 1;
  overflow-y: auto;
  padding: var(--chat-padding);
  background-color: var(--background-color-content);
  display: flex; /* For messages-list to align bottom */
  flex-direction: column;
}

.messages-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
  width: 100%;
  margin-top: auto; /* Pushes messages to bottom on initial load */
}

.message { display: flex; flex-direction: column; max-width: var(--message-max-width); animation: fadeIn 0.2s ease-out; }
.message.user { align-self: flex-end; }
.message.ai { align-self: flex-start; }

.message-content {
  padding: var(--message-padding);
  font-size: 0.9375rem;
  line-height: 1.45;
  word-wrap: break-word;
  overflow-wrap: break-word;
  box-shadow: var(--shadow-sm);
}

.message.user .message-content {
  background-color: var(--primary-color);
  color: white;
  border-radius: var(--message-border-radius) var(--message-border-radius) 0 var(--message-border-radius);
}

.message.ai .message-content {
  background-color: var(--background-color-elements);
  color: var(--text-color-primary);
  border: 1px solid var(--border-color-light);
  border-radius: var(--message-border-radius) var(--message-border-radius) var(--message-border-radius) 0;
}

.message-image { max-width: 100%; border-radius: calc(var(--message-border-radius) - 4px); margin-top: 8px; overflow: hidden; }

.typing-indicator {
  display: flex; align-items: center; padding: var(--message-padding);
  background-color: var(--background-color-elements);
  border: 1px solid var(--border-color-light);
  border-radius: var(--message-border-radius) var(--message-border-radius) var(--message-border-radius) 0;
  width: fit-content; margin: 8px 0; align-self: flex-start;
}
.typing-indicator span {
  width: 8px; height: 8px; border-radius: 50%;
  background-color: var(--text-color-placeholder);
  margin: 0 2px; display: inline-block;
  animation: typing 1.4s infinite ease-in-out both;
}
.typing-indicator span:nth-child(1) { animation-delay: -0.32s; }
.typing-indicator span:nth-child(2) { animation-delay: -0.16s; }

.chat-footer {
  padding: 10px var(--chat-padding);
  background-color: var(--background-color-content);
  border-top: 1px solid var(--border-color-light);
  flex-shrink: 0;
}

.file-preview-wrapper.hidden { display: none; }
.file-preview-wrapper { margin-bottom: 8px; }
.file-preview-item {
  position: relative; display: inline-block;
  background: var(--background-color-elements);
  padding: 4px; border-radius: var(--button-border-radius);
}
.file-preview-item img { max-height: 80px; border-radius: calc(var(--button-border-radius) - 2px); display: block; }
.remove-file-button {
  position: absolute; top: -8px; right: -8px;
  background-color: var(--text-color-secondary);
  color: white; border-radius: 50%; width: 24px; height: 24px;
  display: flex; align-items: center; justify-content: center;
  border: 2px solid var(--background-color-content);
  cursor: pointer; box-shadow: var(--shadow-sm);
}
.remove-file-button:hover { background-color: var(--system-red); }

.input-actions-wrapper {
  display: flex; align-items: flex-end; gap: 8px;
  background-color: var(--background-color-elements);
  border-radius: var(--input-border-radius);
  padding: 8px;
  border: 1px solid var(--border-color-medium);
}
.input-actions-wrapper:focus-within {
  border-color: var(--primary-color);
  box-shadow: 0 0 0 2px rgba(var(--primary-color-rgb), 0.25);
}

#message-input {
  flex: 1; border: none; outline: none; resize: none;
  font-family: inherit; font-size: 1rem;
  line-height: 1.4; padding: 8px 4px;
  background-color: transparent;
  max-height: 120px; scrollbar-width: thin;
  color: var(--text-color-primary);
}
#message-input::placeholder { color: var(--text-color-placeholder); }
#message-input::-webkit-scrollbar { width: 6px; }
#message-input::-webkit-scrollbar-thumb { background-color: var(--border-color-medium); border-radius: 3px; }

.input-action-button {
  background: none; border: none; padding: 6px;
  cursor: pointer; color: var(--text-color-secondary);
  display: flex; align-items: center; justify-content: center;
  border-radius: 50%; flex-shrink: 0;
}
.input-action-button:hover { color: var(--primary-color); }

.send-button-styled {
  background-color: var(--primary-color);
  color: white; border-radius: var(--input-border-radius);
  padding: 8px;
}
.send-button-styled:hover { background-color: #005bb5; }

.modal {
  position: fixed; top: 0; left: 0; width: 100%; height: 100%;
  background-color: rgba(0,0,0,0.4);
  display: flex; align-items: center; justify-content: center;
  z-index: 2000; opacity: 0; visibility: hidden;
  transition: opacity 0.2s ease, visibility 0.2s ease;
}
.modal.hidden { opacity: 0; visibility: hidden; pointer-events: none; }
.modal.visible { opacity: 1; visibility: visible; pointer-events: auto; }

.modal-content {
  background-color: var(--background-color-content);
  border-radius: 14px; width: 90%; max-width: 400px;
  max-height: 90vh; overflow-y: auto;
  box-shadow: 0 10px 25px rgba(0,0,0,0.1);
  transform: scale(0.95); transition: transform 0.2s ease;
}
.modal.visible .modal-content { transform: scale(1); }

.modal-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 16px; border-bottom: 1px solid var(--border-color-light);
}
.modal-header h2 { font-size: 1.1rem; font-weight: 600; }
.modal-close-button { background: none; border: none; padding: 4px; cursor: pointer; color: var(--text-color-secondary); }
.modal-close-button:hover { color: var(--text-color-primary); }

.modal-body { padding: 16px; }
.settings-section h3 { font-size: 1rem; font-weight: 600; margin-bottom: 8px; }
.form-group { margin-bottom: 16px; }
.form-group label { display: block; margin-bottom: 6px; font-weight: 500; font-size: 0.875rem; }
.form-group input {
  width: 100%; padding: 10px 12px;
  border: 1px solid var(--border-color-medium);
  border-radius: var(--button-border-radius);
  font-size: 0.9375rem;
  background-color: var(--background-color-elements);
  color: var(--text-color-primary);
}
.form-group input:focus {
  outline: none; border-color: var(--primary-color);
  box-shadow: 0 0 0 2px rgba(var(--primary-color-rgb), 0.25);
  background-color: var(--background-color-content);
}

.primary-button {
  background-color: var(--primary-color); color: white;
  padding: 10px 16px; border-radius: var(--button-border-radius);
  font-weight: 500; width: 100%; display: flex; justify-content: center;
  font-size: 0.9375rem; border: none; cursor: pointer;
  transition: background-color 0.15s ease;
}
.primary-button:hover { background-color: #005bb5; }

.notification-banner {
  position: fixed; top: 0; left: 0; right: 0;
  padding: 12px var(--chat-padding);
  color: white; text-align: center; z-index: 3000;
  transform: translateY(-120%);
  transition: transform 0.35s ease-in-out;
  font-size: 0.875rem; font-weight: 500;
  box-shadow: var(--shadow-md);
}
.notification-banner.hidden {transform: translateY(-120%);}
.notification-banner.visible { transform: translateY(0); }
.notification-banner.info { background-color: var(--system-blue); }
.notification-banner.success { background-color: var(--system-green); }
.notification-banner.error { background-color: var(--system-red); }

@keyframes fadeIn { from { opacity: 0; transform: translateY(5px); } to { opacity: 1; transform: translateY(0); } }
@keyframes typing { 0%, 80%, 100% { transform: scale(0.7); opacity: 0.5; } 40% { transform: scale(1); opacity: 1; } }

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border-width: 0;
}

/* --- Mobile and Dark Mode Adjustments --- */
@media (max-width: 768px) {
  .chat-list-sidebar {
    position: fixed; left: 0; top: 0; bottom: 0;
    z-index: 1500;
    margin-left: -260px; /* Hidden off-screen by default */
    box-shadow: var(--shadow-md);
    /* transition: margin-left 0.3s ease-in-out; */ /* Already defined above, ensure it's consistent */
  }
  .chat-list-sidebar.visible { 
    margin-left: 0; /* Slide in */ 
  }
  
  /* .sidebar-toggle is now always flex, so no need for specific mobile rule here */
  /* #toggle-sidebar-button specific margin is fine if still needed for mobile spacing */

  .app-main-content {
    /* On mobile, main content takes full width when sidebar is hidden or overlaid */
    /* transition: margin-left 0.3s ease-in-out; */ /* Not needed if sidebar is an overlay */
  }

  .app-wrapper.sidebar-visible .app-main-content {
    /* Optional: If you want to dim or slightly push main content when sidebar is open over it */
    /* margin-left: 260px; /* Example if sidebar pushes content */
  }


  .app-header {
    padding: 0 10px; /* Overall header padding */
    /* justify-content: space-between; */ /* Already set above */
  }

  /* Override for app-title specifically within mobile header */
  /* .app-header .app-title { ... } */ /* app-title is removed */

  .header-center-item { /* Adjustments for model selector on mobile */
    flex-grow: 1; /* Allow it to take available space */
    min-width: 120px; /* Prevent it from becoming too small */
    justify-content: flex-start; /* Align select to left on mobile */
  }

  .header-actions {
    display: flex; 
    align-items: center; 
    gap: 4px; /* Reduce gap for mobile */
    flex-shrink: 0; /* Prevent actions from being squeezed out */
  }

  .model-selector-wrapper select {
    min-width: 0; /* Allow to shrink more on mobile */
    max-width: none; /* Remove max-width for mobile to allow flexibilty */
    width: 100%; /* Make it take width of its wrapper on mobile */
    padding: 8px 28px 8px 10px; /* Mobile padding, ensure space for arrow on right */
    font-size: 0.8rem; /* Smaller font in select for mobile */
    height: 38px; /* Consistent height */
    text-align: left; /* Align text to left in mobile select */
  }

  .header-button { /* General tap target for header buttons on mobile */
    padding: 8px; 
    font-size: 1.1rem; /* Make icons in buttons slightly larger if they are font icons */
  }

  .message { 
    --message-max-width: 95%; /* Slightly more width for messages */
    padding: 8px 10px; /* Adjust message padding */
    font-size: 0.9rem; /* Slightly smaller message font if needed */
  }
  .message-content code, .message-content pre {
    font-size: 0.8rem; /* Adjust code block font size on mobile */
  }
  
  /* Modal adjustments for mobile */
  .modal {
    padding: 10px; /* Add some padding so modal doesn't touch screen edges */
  }
  .modal-content {
    width: 100%; /* Use full available width within modal padding */
    max-width: 450px; /* But don't let it get too wide on larger "mobile" screens */
    max-height: 90vh; /* Limit height */
  }

  .modal-header h2 {
    font-size: 1.1rem; /* Adjust modal title size */
  }

  .modal-body {
    padding: 15px; /* Adjust modal body padding */
  }

  .form-group label {
    font-size: 0.875rem; 
    margin-bottom: 4px;
  }
  .form-group input[type="text"], .form-group input[type="password"] {
    padding: 10px 12px; 
    font-size: 0.9rem; 
  }
  .modal-footer .primary-button { 
    padding: 10px 16px; 
    font-size: 0.9rem; 
    width: 100%; /* Make modal buttons full width for easier tapping */
    margin-top: 5px;
  }
   .modal-footer .primary-button + .primary-button { /* If multiple buttons in footer */
       margin-top: 10px;
   }

  .chat-footer {
    padding: 8px;
  }
  #message-input {
    padding: 8px 4px; /* Re-evaluate padding with new font size */
    font-size: 16px; /* Prevent iOS zoom on focus */
    line-height: 1.4; /* Ensure consistent line height */
    /* max-height already defined, should be fine */
  }
  .input-actions-wrapper {
    /* May need slight padding adjustment if input height changes significantly */
  }
  .input-actions-wrapper button {
    padding: 8px;
    font-size: 1.2rem; /* Ensure send/upload icons are big enough */
  }
  #file-preview-container {
    bottom: calc(var(--footer-height, 60px) + 10px); /* Adjust based on actual footer height */
  }
}

/* Desktop specific sidebar adjustments for collapsibility */
@media (min-width: 769px) {
  .chat-list-sidebar {
    /* Default state is visible on desktop, transition margin for collapse */
    transition: margin-left 0.3s ease-in-out, width 0.3s ease-in-out;
  }
  .chat-list-sidebar:not(.visible) {
    margin-left: -260px; /* Slide out like mobile */
    /* Optionally, if you want it to shrink to zero width instead of sliding:
    width: 0;
    min-width: 0;
    padding-left: 0;
    padding-right: 0;
    overflow: hidden; 
    */
  }
  /* When sidebar is not visible, app-main-content takes full width */
  .app-wrapper:not(.sidebar-visible) .app-main-content {
     /* This ensures main content expands if sidebar was pushing it */
  }
  .app-wrapper.sidebar-visible .app-main-content {
    /* If sidebar pushes content, adjust margin. If it overlays, this isn't needed or is 0 */
    /* margin-left: 260px; */ /* Example: if sidebar pushes */
  }
}

@media (prefers-color-scheme: dark) {
  :root {
    --text-color-primary: #f5f5f7;
    --text-color-secondary: #8e8e93;
    --text-color-placeholder: #636366;
    --background-color-page: #000000; /* True black page background */
    --background-color-content: #121212; /* Main chat area and modal background */
    --background-color-elements: #1e1e1e;  /* Sidebar, inputs, AI messages */
    
    --border-color-light: #2c2c2e;
    --border-color-medium: #3a3a3c;
    --primary-color_01: rgba(0,122,255,0.2); 
    --primary-color_03: rgba(0,122,255,0.5);
    --system-red-rgb: 255, 59, 48; /* Added for rgba hover effect */
  }

  .chat-list-sidebar {
    background-color: var(--background-color-elements); 
    border-right-color: var(--border-color-light);
  }
  .sidebar-header, .app-header, .chat-footer {
    background-color: var(--background-color-content); 
    border-color: var(--border-color-light);
  }
  .model-selector-wrapper select {
    background-color: var(--background-color-elements);
    border-color: var(--border-color-medium);
    color: var(--text-color-primary);
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='%238e8e93' viewBox='0 0 16 16'%3E%3Cpath fill-rule='evenodd' d='M1.646 4.646a.5.5 0 0 1 .708 0L8 10.293l5.646-5.647a.5.5 0 0 1 .708.708l-6 6a.5.5 0 0 1-.708 0l-6-6a.5.5 0 0 1 0-.708z'/%3E%3C/svg%3E");
  }
  .model-selector-wrapper select:focus {
    box-shadow: 0 0 0 2px rgba(var(--primary-color-rgb), 0.4); /* Dark mode focus */
  }
  .chat-main-area {
      background-color: var(--background-color-content);
  }
  .message.ai .message-content {
    background-color: var(--background-color-elements);
    border-color: var(--border-color-medium);
  }
  .input-actions-wrapper {
    background-color: var(--background-color-elements);
    border-color: var(--border-color-medium);
  }
  #message-input { 
      color: var(--text-color-primary); 
      background-color: transparent; /* Ensure it inherits from wrapper */
  }
  #message-input::placeholder { color: var(--text-color-placeholder); }
  .input-action-button { color: var(--text-color-secondary); }
  .input-action-button:hover { color: var(--primary-color); }
  
  .remove-file-button { background-color: var(--text-color-secondary); border-color: var(--background-color-content);}
  .remove-file-button:hover { background-color: var(--system-red); }
  
  .modal-content { background-color: var(--background-color-elements); }
  .form-group input {
      background-color: var(--background-color-content);
      border-color: var(--border-color-medium);
      color: var(--text-color-primary);
  }
  .form-group input:focus {
      background-color: var(--background-color-content);
      border-color: var(--primary-color);
  }

  .chat-list-item:hover {
    background-color: var(--primary-color_01);
    color: var(--primary-color);
  }
  .chat-list-item.active {
    background-color: var(--primary-color);
    color: #ffffff; 
    border-color: var(--primary-color);
  }
} 