/* style.css ---------------------------------------------------- */

/* Google Font */
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap');

/* ----------  COLOR & FONT VARIABLES  ---------- */
:root {
  --bg-primary: #1e1e1e;
  --bg-secondary: #252526;
  --bg-tertiary: #333;
  --bg-hover: #444;
  --bg-active: #0e639c;              /* active-chat badge */
  --text-primary: #d4d4d4;
  --text-secondary: #ccc;
  --text-placeholder: #888;
  --accent-color: #0e639c;
  --accent-hover: #1177bb;
  --border-color: #4a4a4a;
  --code-bg: #1a1a1a;
  --error-color: #f44336;
  --delete-color: #c7524a;
  --delete-hover: #e06c5c;
  --font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI',
                 Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue',
                 sans-serif;
  --link-color: #61dafb;
  --sidebar-width: 280px;
  --sidebar-transition: transform 0.3s ease-in-out, width 0.3s ease-in-out;
}

/* reset + base */
* { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 16px; }
body {
  font-family: var(--font-family);
  background: var(--bg-primary);
  color: var(--text-primary);
  line-height: 1.6;
  height: 100vh;
  overflow: hidden;
  display: flex;
}

/* ----------  LAYOUT  ---------- */
.container { display: flex; width: 100%; height: 100%; position: relative; }

/* sidebar */
.sidebar {
  width: var(--sidebar-width);
  background: var(--bg-secondary);
  border-right: 1px solid var(--border-color);
  padding: 15px;
  display: flex; flex-direction: column;
  overflow-y: hidden;
  flex-shrink: 0;
  transition: var(--sidebar-transition);
  position: relative;
}
.sidebar.closed {
  width: 0;
  padding: 0;
  border-right: none;
  transform: translateX(-100%);
  overflow: hidden;
}

/* ==================================================== *
 * UNIVERSAL BUTTON/ICON HELPERS
 * ==================================================== */
.btn-icon {
  background: none;
  border: none;
  color: var(--text-secondary);
  display: flex; align-items: center; justify-content: center;
  line-height: 1; cursor: pointer;
  padding: 0;
  flex-shrink: 0; /* Ensure buttons don't shrink */
  transition: color 0.2s ease, background-color 0.2s ease;
}
.btn-icon:hover:not(:disabled) { color: var(--text-primary); }
.btn-icon:disabled { color: #666; cursor: not-allowed; }
.btn-lg         { font-size: 2rem; padding: 6px; }
.btn-icon svg   { width: 24px; height: 24px; }


/* --------------------------------------------------- *
 * SIDEBAR TOGGLE BUTTONS (OPEN & CLOSE) – UPDATED
 * --------------------------------------------------- */
.sidebar-toggle-open,
.sidebar-toggle-close {
  background: none;
  border: none;
  color: var(--text-secondary);
  display: none; /* Initially hidden, JS controls visibility */
  position: absolute;
  font-size: 2.2rem; /* Increased size for easier touch */
  z-index: 1;
  padding: 6px;
  cursor: pointer;
  transition: color 0.2s ease;
  /* Ensure these are clickable even when sidebar content overflows */
  align-self: flex-start; /* Keep it top left/right */
}
.sidebar-toggle-open:hover,
.sidebar-toggle-close:hover {
  color: var(--text-primary);
}

/* Sidebar is open: show “close” at top-right */
.sidebar:not(.closed) .sidebar-toggle-close {
  display: flex; /* Use flex for consistency */
  top: 10px;
  right: 10px;
}

/* Sidebar is closed: show “open” at top-left (via JS now) */
/* JS will handle toggling display based on state */

/* ==================================================== *
 * SLIDER (SWITCH) STYLING
 * ==================================================== */
.switch-setting       { display: flex; align-items: center; gap: 10px; }
.switch-label         { color: var(--text-secondary); font-size: 0.9rem; }

.switch { position: relative; display: inline-block; width: 46px; height: 24px; }
.switch input { opacity: 0; width: 0; height: 0; }
.slider {
  position: absolute; cursor: pointer;
  top: 0; left: 0; right: 0; bottom: 0;
  background: var(--bg-tertiary);
  transition: .4s;
  border-radius: 34px;
}
.slider:before {
  position: absolute; content: "";
  height: 18px; width: 18px;
  left: 3px; bottom: 3px;
  background: var(--text-secondary);
  transition: .4s; border-radius: 50%;
}
input:checked + .slider       { background: var(--accent-color); }
input:focus   + .slider       { box-shadow: 0 0 1px var(--accent-color); }
input:checked + .slider:before{ transform: translateX(22px); background: #fff; }

/* ==================================================== *
 * CHAT LIST & NEW CHAT BUTTON
 * ==================================================== */
.new-chat-button {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  width: 100%; padding: 10px 15px; margin-bottom: 15px;
  background: var(--bg-tertiary); border: 1px solid var(--border-color);
  color: var(--text-primary); border-radius: 5px; font-size: 0.95rem;
  cursor: pointer; text-align: center; transition: background 0.2s ease;
}
.new-chat-button:hover { background: var(--bg-hover); }
.new-chat-button svg   { margin-top: -1px; }

.chat-list-container { flex-grow: 1; overflow-y: auto; margin-bottom: 15px; }
#chat-list           { list-style: none; }
#chat-list li {
  padding: 8px 12px; margin-bottom: 5px; border-radius: 4px;
  cursor: pointer; transition: background 0.2s ease, color 0.2s ease;
  font-size: 0.9rem; color: var(--text-secondary);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  position: relative;
}
#chat-list li:hover            { background: var(--bg-hover); color: var(--text-primary); }
#chat-list li.active           { background: var(--bg-active); color: #fff; font-weight: 500; }
#chat-list li .delete-chat-button {
  position: absolute; top: 50%; right: 8px; transform: translateY(-50%);
  background: none; border: none; color: var(--text-secondary);
  font-size: 1rem; line-height: 1; cursor: pointer; padding: 2px;
  opacity: 0; transition: opacity 0.2s ease, color 0.2s ease;
}
#chat-list li:hover .delete-chat-button  { opacity: 0.7; }
#chat-list li.active .delete-chat-button { color: #eee; }
#chat-list li .delete-chat-button:hover  { opacity: 1; color: var(--delete-color); }

/* settings */
.sidebar-section { margin-top: 15px; padding-top: 15px; border-top: 1px solid var(--border-color); }
.setting         { margin-bottom: 15px; }
.setting label {
  display: block; font-size: .9rem; color: var(--text-secondary); margin-bottom: 6px;
}
.setting select,
.setting input[type="checkbox"] {
  width: 100%; padding: 8px 10px; background: var(--bg-tertiary);
  border: 1px solid var(--border-color); color: var(--text-primary);
  border-radius: 4px; font-size: .9rem;
}
.setting select:focus {
  outline: none; border-color: var(--accent-color);
  box-shadow: 0 0 0 2px rgba(14, 99, 156, .3);
}
.setting input[type="checkbox"] {
  width: auto; margin-right: 8px; accent-color: var(--accent-color); vertical-align: middle;
}

/* ==================================================== *
 * RED “RESET DATA” BUTTON
 * ==================================================== */
.reset-data-button {
  display: flex; align-items: center; justify-content: center; gap: 6px;
  width: 100%; padding: 9px 12px;
  background: var(--delete-color);
  color: #fff; border: none; border-radius: 4px;
  cursor: pointer; font-size: 0.9rem;
  transition: background .2s ease;
}
.reset-data-button svg { flex-shrink: 0; }
.reset-data-button:hover { background: var(--delete-hover); }

.sidebar-footer { margin-top: auto; padding-top: 10px; font-size: 0.8rem; color: var(--text-secondary); }

/* ---------- CHAT AREA ---------- */
.chat-area {
  flex-grow: 1; display: flex; flex-direction: column; height: 100vh;
  background: var(--bg-primary);
  position: relative; /* Needed for absolute positioned children like overlay */
  transition: margin-left 0.3s ease-in-out; /* Added for sidebar toggle */
}

.chat-header {
  padding: 10px 20px; border-bottom: 1px solid var(--border-color);
  background: var(--bg-secondary); font-size: .9rem; color: var(--text-secondary);
  display: flex; justify-content: space-between; align-items: center;
  min-height: 45px; /* Ensure consistent height */
  flex-shrink: 0; /* Prevent header from shrinking */
}
#current-chat-title { font-weight: 500; color: var(--text-primary); }

.chat-history {
  flex-grow: 1; overflow-y: auto; padding: 20px;
  display: flex; flex-direction: column; gap: 15px;
}
.initial-prompt {
  text-align: center; color: var(--text-secondary); margin: auto; font-size: 1.1rem;
  padding: 20px;
}

/* ---------- MESSAGE BUBBLES ---------- */
.message {
  max-width: 85%; padding: 12px 18px; border-radius: 18px; word-wrap: break-word;
  line-height: 1.5; opacity: 0; animation: fadeIn .3s ease forwards;
}
@keyframes fadeIn { to { opacity: 1; } }

.message.user {
  background: var(--accent-color); color: #fff; border-bottom-right-radius: 5px;
  align-self: flex-end; margin-left: auto;
}
.message.assistant {
  background: var(--bg-secondary); color: var(--text-primary);
  border: 1px solid var(--border-color); border-bottom-left-radius: 5px;
  align-self: flex-start; margin-right: auto; text-align: left;
}
/* Make sure images within messages don't overflow */
.message.user > span,
.message.assistant > div {
  word-break: break-word; /* Ensure long words break */
}
.message.user img,
.message.assistant img {
  max-width: 100%; /* Allow image to shrink */
  max-height: 250px; /* Limit height */
  height: auto; /* Maintain aspect ratio */
  border-radius: 6px; margin: 8px 0; /* Add some space */
  object-fit: contain; display: block;
  cursor: pointer; border: 1px solid var(--border-color);
  background-color: var(--bg-tertiary); /* Added background for transparency */
}
.message.assistant.streaming::after {
  content: '▋'; display: inline-block; animation: blink 1s step-end infinite;
  margin-left: 2px; opacity: .7;
}
@keyframes blink { 50% { opacity: 0; } }
.message.error {
  background: rgba(244, 67, 54, .2); color: var(--error-color);
  border: 1px solid var(--error-color); align-self: center; max-width: 95%;
  text-align: center; border-radius: 5px; padding: 8px 12px; font-size: 0.9rem;
}

/* ----------  RICH-FORMAT STYLES  ---------- */
.assistant-message.chatgpt-format pre {
  background: #282c34; /* Slightly different code bg */
  color: #abb2bf; padding: 1em;
  border-radius: 5px; overflow-x: auto; font-family: 'Courier New', monospace;
  font-size: .9em; margin: 10px 0;
  border: 1px solid var(--border-color); /* Add border */
}
.assistant-message.chatgpt-format code:not(pre>code) {
  background: var(--bg-tertiary); color: var(--text-primary);
  padding: .2em .4em; border-radius: 3px; font-size: .9em;
  border: 1px solid var(--border-color); /* Subtle border */
}
.assistant-message.chatgpt-format blockquote {
  border-left: 3px solid var(--border-color); padding-left: 10px;
  margin: 10px 0; /* Adjusted margin */
  color: #bbb; font-style: italic;
}
.assistant-message.chatgpt-format ul,
.assistant-message.chatgpt-format ol { padding-left: 25px; margin: 10px 0; } /* Increased indent */
.assistant-message.chatgpt-format li { margin-bottom: 4px; }

.assistant-message.chatgpt-format h1,
.assistant-message.chatgpt-format h2,
.assistant-message.chatgpt-format h3,
.assistant-message.chatgpt-format h4,
.assistant-message.chatgpt-format h5,
.assistant-message.chatgpt-format h6 {
  margin: 1.2em 0 .6em; /* Adjusted margins */
  font-weight: 600; color: var(--text-primary);
  border-bottom: 1px solid var(--border-color); /* Add subtle separator */
  padding-bottom: 0.2em;
}
.assistant-message.chatgpt-format p { margin-bottom: .8em; }
.assistant-message.chatgpt-format a { color: var(--link-color); text-decoration: none; }
.assistant-message.chatgpt-format a:hover { text-decoration: underline; }
.assistant-message.chatgpt-format table {
  border-collapse: collapse; margin: 1em 0; width: auto; /* Changed width */
  border: 1px solid #555; max-width: 100%; overflow-x: auto; /* Ensure table fits */
  display: block; /* Needed for overflow-x */
}
.assistant-message.chatgpt-format th,
.assistant-message.chatgpt-format td {
  border: 1px solid #555; padding: 8px 12px; /* Increased padding */
  text-align: left; color: var(--text-primary);
}
.assistant-message.chatgpt-format th { background: #333; color: #fff; font-weight: bold; }

/* ---------- INPUT AREA ---------- */
.chat-input-area {
  padding: 15px 20px; border-top: 1px solid var(--border-color);
  background: var(--bg-secondary);
  flex-shrink: 0; /* Prevent shrinking */
}
.chat-input-area form { display: flex; align-items: flex-end; gap: 10px; }

/* NEW: Attach Button Styles */
#attach-button {
  background: var(--bg-tertiary);
  border: 1px solid var(--border-color);
  color: var(--text-secondary);
  border-radius: 50%;
  width: 44px;
  height: 44px;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  transition: background .2s ease, border-color .2s ease;
  flex-shrink: 0;
}
#attach-button:hover:not(:disabled) {
  background: var(--bg-hover);
  border-color: var(--accent-hover);
  color: var(--text-primary);
}
#attach-button:disabled {
  background: #2a2a2d;
  border-color: #4a4a4a;
  cursor: not-allowed;
  opacity: .6;
}
#attach-button svg {
  width: 20px; /* Slightly smaller icon */
  height: 20px;
}
/* END NEW */

#chat-input {
  flex-grow: 1; min-height: 44px; max-height: 200px; padding: 10px 15px;
  background: var(--bg-tertiary); border: 1px solid var(--border-color);
  color: var(--text-primary); border-radius: 22px; font-size: 1rem;
  resize: none; overflow-y: auto; line-height: 1.4; font-family: inherit; outline: none;
  transition: border-color 0.2s ease, box-shadow 0.2s ease; /* Added transitions */
}
#chat-input:focus {
  border-color: var(--accent-color);
  box-shadow: 0 0 0 2px rgba(14, 99, 156, .3);
}
#chat-input::placeholder { color: var(--text-placeholder); }
#chat-input:disabled { background-color: #2a2a2d; cursor: not-allowed; opacity: 0.7; }

#send-button {
  background: var(--accent-color); border: none; color: #fff; border-radius: 50%;
  width: 44px; height: 44px; display: flex; justify-content: center; align-items: center;
  cursor: pointer; transition: background .2s ease; flex-shrink: 0;
}
#send-button:hover:not(:disabled) { background: var(--accent-hover); }
#send-button:disabled { background: var(--bg-tertiary); cursor: not-allowed; opacity: .6; border: 1px solid var(--border-color); }
#send-button svg      { fill: #fff; }

/* image preview */
.image-preview-container {
  display: flex; align-items: center; gap: 10px; margin-bottom: 10px;
}
.image-preview-container img {
  max-height: 60px; max-width: 100px; border-radius: 4px; border: 1px solid var(--border-color);
  object-fit: cover; background-color: var(--bg-tertiary); /* Added background */
}
.image-preview-container button {
  background: var(--delete-color); color: #fff; border: none; border-radius: 50%;
  width: 22px; height: 22px; font-size: 13px; line-height: 20px; /* Slightly larger */
  cursor: pointer; padding: 0; flex-shrink: 0; display: flex; align-items: center; justify-content: center;
  transition: background-color 0.2s ease;
}
.image-preview-container button:hover { background-color: var(--delete-hover); }

/* drag overlay */
.drop-overlay {
  position: absolute; top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(14, 99, 156, .8); color: #fff;
  display: flex; flex-direction: column; /* Center icon + text */
  justify-content: center; align-items: center;
  font-size: 1.5rem; font-weight: bold; text-align: center; pointer-events: none;
  opacity: 0; transition: opacity .2s ease; z-index: 10; padding: 20px;
}
.drop-overlay::before { /* Add an icon */
  content: "🖼️"; /* Use an emoji or SVG here */
  font-size: 3rem; margin-bottom: 10px;
}
.chat-area.drag-over .drop-overlay { opacity: 1; }

/* cookie notice */
.cookie-notice {
  position: fixed; bottom: 0; left: 0; width: 100%;
  background: var(--bg-tertiary); color: var(--text-secondary);
  padding: 15px 20px; border-top: 1px solid var(--border-color);
  display: flex; justify-content: space-between; align-items: center;
  font-size: .9rem; z-index: 100; opacity: 0;
  transform: translateY(100%);
  transition: opacity .5s ease, transform .5s ease;
}
.cookie-notice.show { opacity: 1; transform: translateY(0); }
#accept-cookies {
  background: var(--accent-color); color: #fff; border: none;
  padding: 8px 15px; border-radius: 4px; cursor: pointer; transition: background .2s ease; flex-shrink: 0; margin-left: 15px;
}
#accept-cookies:hover { background: var(--accent-hover); }

/* scrollbars */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: var(--bg-secondary); }
::-webkit-scrollbar-thumb {
  background: var(--bg-tertiary); border-radius: 4px; border: 2px solid var(--bg-secondary);
}
::-webkit-scrollbar-thumb:hover { background: var(--bg-hover); }
::-webkit-scrollbar-corner { background: var(--bg-secondary); }


/* ---------- RESPONSIVE ---------- */
@media (max-width: 768px) {
  .sidebar {
    position: fixed; /* Changed from absolute */
    left: 0; top: 0; bottom: 0;
    z-index: 20; /* Ensure sidebar is above chat area */
    transform: translateX(-105%); /* Start fully off-screen */
    width: 85%; max-width: 300px;
    box-shadow: 2px 0 5px rgba(0,0,0,.2);
    border-right: 1px solid var(--border-color); /* Keep border */
    padding: 15px; /* Restore padding */
    transition: var(--sidebar-transition); /* Use variable */
  }
  .sidebar.open   { transform: translateX(0); }
  /* .closed state is now the default transform */
  .sidebar.closed {
      /* No need for explicit closed styles if default is closed */
      /* But keep width/padding for calculation if needed, though transform is better */
      width: 85%; /* Keep width consistent for transition */
      padding: 15px; /* Keep padding consistent */
  }

  .chat-area      { margin-left: 0 !important; width: 100%; /* Ensure chat area takes full width */ }

  /* Sidebar Toggle Button Handling */
  .sidebar-toggle-open {
    display: block; /* Always potentially visible */
    position: absolute; /* Position relative to chat-header */
    top: 50%; transform: translateY(-50%); /* Center vertically */
    left: 15px;
    z-index: 1; /* Above chat header content */
  }
  .sidebar-toggle-close {
    display: flex; /* Match display type */
    /* position: absolute; /* Already absolute inside sidebar */
    /* top: 10px; */
    /* right: 10px; */
  }

  /* Adjust visibility based on sidebar state (handled by JS preferably or CSS :not selector) */
  /* Example using JS logic (preferred): JS adds/removes a class or style */
  /* Example using CSS: */
  /* .sidebar.open ~ .chat-area .sidebar-toggle-open { display: none; } */
  /* .sidebar:not(.open) ~ .chat-area .sidebar-toggle-open { display: block; } */

  .message { max-width: 95%; }
  .chat-input-area { padding: 10px; }
  .chat-header { padding-left: 60px; position: relative; /* Needed for absolute positioning of toggle */ }
  .chat-history { padding: 15px; }

  /* Adjust input area on small screens */
  .chat-input-area form { gap: 8px; }
  #attach-button, #send-button { width: 40px; height: 40px; }
  #chat-input { min-height: 40px; padding: 8px 12px; }
  #attach-button svg { width: 18px; height: 18px; }
}

@media (max-width: 480px) {
    /* Further adjustments for very small screens */
    .chat-header { padding-left: 55px; padding-right: 15px; }
    .sidebar-toggle-open { left: 10px; }
    .chat-input-area { padding: 8px; }
    .chat-input-area form { gap: 5px; }
    #attach-button, #send-button { width: 38px; height: 38px; }
    #chat-input { min-height: 38px; font-size: 0.95rem; }
}