/** dark-mode syntax-highlighting using monokai theme (by default) */
@import url("https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.7.2/styles/monokai.min.css");
/** light-mode syntax-highlighting theme using solarized light */
@import url("https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.7.2/styles/solarized-light.min.css")
  screen and (prefers-color-scheme: light);

* {
  box-sizing: border-box;
}
:root {
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
    "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue",
    sans-serif;
  /* Default colour scheme: dark-mode */
  --bg-color: #000;
  --text-color: #fff;
  --pre-hover: #fff1;
  --accent-color: #8d48c5;
  --abort-color: red;
  --sidebar-bg: #111;
  --sidebar-hover: #222;
  --sidebar-width: 250px;
}

body {
  margin: 0;
  padding: 0;
  height: 100vh;
  height: 100dvh;
  background-color: var(--bg-color);
  color: var(--text-color);
}

#app-container {
  display: flex;
  height: 100%;
}

/* Chat sidebar styles */
#chat-sidebar {
  position: fixed; /* Changed from absolute for better mobile behavior */
  top: 0;
  transform: translateX(-100%); /* Start hidden off-screen */
  width: 300px;
  height: 100%;
  border-right: 1px solid var(--accent-color);
  background-color: var(--sidebar-bg);
  transition: transform 0.3s ease-in-out;
  z-index: 1000; /* Ensure sidebar is above main content */
  display: flex;
  flex-direction: column;
  overflow-y: auto; /* Allow scrolling if content overflows */
}

#chat-sidebar.open {
  transform: translateX(0); /* Slide in when open */
}

.sidebar-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem;
  border-bottom: 1px solid var(--accent-color);
}

.sidebar-header h3 {
  margin: 0;
  font-size: 1.1rem;
}

#new-chat-btn {
  padding: 0.5rem 1rem;
  cursor: pointer;
  border-radius: 4px;
  border: 1px solid var(--accent-color);
  background-color: var(--accent-color);
  color: white;
}

#chat-list {
  flex-grow: 1;
  overflow-y: auto;
}

.chat-item {
  padding: 0.75rem 1rem;
  border-bottom: 1px solid #eee;
  border-right: 5px solid transparent;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.5rem; /* Add gap between elements */
}

.chat-item:hover {
  border-right-color: #e9ecef;
}

.chat-item.active {
  border-right-color: var(--accent-color);
  font-weight: bold;
}

.chat-item-summary {
  flex-grow: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-item-date {
  font-size: 0.8em;
  color: #6c757d;
  white-space: nowrap;
}

.chat-item-actions {
  display: flex;
}

.delete-chat-btn {
  background: none;
  border: none;
  cursor: pointer;
  padding: 0.2rem;
  font-size: 0.9rem;
  color: #dc3545;
  opacity: 0.6;
}

.delete-chat-btn:hover {
  opacity: 1;
  color: #a02531;
}

.empty-chat-list {
  padding: 1rem;
  text-align: center;
  color: #6c757d;
}

/* Sidebar Toggle Button */
#sidebar-toggle {
  position: fixed;
  top: 10px;
  left: 10px;
  z-index: 1001; /* Above sidebar */
  /* background-color: var(--accent-color); */
  border: 1px solid var(--accent-color);
  border-radius: 4px;
  padding: 5px 10px;
  cursor: pointer;
  font-size: 1.2rem;
}

/* Main Content Adjustments */
#main-content {
  transition: margin-left 0.3s ease-in-out;
  padding-top: 50px; /* Add padding to prevent overlap with fixed toggle */
}

/* Responsive adjustments */
@media (max-width: 768px) {
  #app-container {
    position: relative;
  }

  #chat-sidebar {
    position: fixed; /* Back to fixed for mobile layout */
    top: 0;
    bottom: 0;
    z-index: 10;
  }

  #sidebar-toggle {
    position: fixed; /* Fixed position for mobile */
    top: 10px;
    left: 10px;
    z-index: 1001; /* Above sidebar */
    border: 1px solid var(--accent-color);
    border-radius: 4px;
    padding: 5px 10px;
    cursor: pointer;
    font-size: 1.2rem;
  }

  #main-content {
    margin-left: 0 !important; /* Ensure no margin is applied */
  }

  #chat-sidebar.open + #main-content {
    /* Optional: Add overlay or dim effect when sidebar is open */
    /* filter: brightness(0.5); */
  }
}

/* Desktop view: Sidebar is visible by default, content shifts */
@media (min-width: 768px) {
  #chat-sidebar {
    position: relative; /* Back to relative for desktop layout */
    left: 0; /* Always visible */
    height: 100vh; /* Full height */
    transition: width 0.3s ease-in-out, margin-left 0.3s ease-in-out; /* Animate width */
  }

  #app-container {
    display: flex;
  }

  #main-content {
    flex-grow: 1;
    margin-left: 0; /* Reset margin */
    padding-top: 0; /* Reset padding */
    transition: margin-left 0.3s ease-in-out;
  }

  #sidebar-toggle {
    position: static; /* Normal position in flow */
    margin: 1rem 0 0 1rem; /* Adjust margin */
    order: -1; /* Place toggle before sidebar header content visually if needed */
    /* Or integrate it into the header */
  }

  /* Style for when sidebar is collapsed on desktop */
  #chat-sidebar:not(.open) {
    display: none; /* Hide sidebar */
  }

  #main-content.sidebar-collapsed {
    margin-left: 0; /* Main content takes full width */
  }

  /* Adjust toggle button position/style for desktop */
  .sidebar-header {
    /* Optional: Adjust header if toggle is inside */
  }
}

/* Main content styles */
main {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  height: 100dvh;
  flex: 1;
  padding-left: 2ch;
  max-width: 80ch;
  margin: 0 auto;
}

form {
  display: flex;
  justify-content: stretch;
  margin-top: auto;
}

/* styles for small-screens */
@media (max-width: 500px) {
  form {
    margin-left: -2ch;
    width: 100vw;
    width: 100dvw;
  }
}

textarea {
  flex-grow: 1;
}

output {
  /* line-clamp 4 lines */
  display: -webkit-box;
  -webkit-line-clamp: 4;
  -webkit-box-orient: vertical;
  overflow: hidden;
  /* style */
  padding: 1rem;
  border-left: 0.5ch solid;
  border-radius: 0;
  padding-left: 1.5ch;
  margin-left: -2ch;
}

#messages {
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  /*
    This prevents overflow-y from triggering scrollbar
    justify-content: flex-end;

    Instead of shoving small amounts of content to the bottom with flex-end, we'll
    put a auto margin-top on the first (visible) element instead.
    */
  flex: 1 1 0;
  min-height: 0;
  gap: 1rem;
  overflow-y: auto;
  scrollbar-gutter: stable;
  padding-left: 2ch;
  margin-left: -2ch;
}

.message {
  position: relative;
}
.message:nth-child(2) {
  margin-top: auto;
}
.message > :first-child {
  margin-top: 0;
}
.message-system {
  display: none;
}
.message::before {
  display: inline-block;
  position: absolute;
  left: -2ch;
}
.message-assistant::before {
  content: "🤖";
}
.message-user {
  white-space: pre-wrap;
  white-space: break-spaces;
}
.message-user::before {
  content: "👤";
}

.message.draft-message {
  /* Renamed from preview-message */
  opacity: 0.6;
  /* Add any other specific draft styles */
}

/* Optional: Style for streaming messages if desired */
.message.streaming-message {
  /* e.g., slightly different background or a subtle animation */
}

/* :hover style for pre element to show copy button */
pre {
  position: relative;
}
pre::before {
  display: none;
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
  content: "📋";
  font-size: 0.75rem;
  cursor: pointer;
}
pre:hover {
  background-color: var(--pre-hover);
}
pre:hover::before {
  display: inline-block;
}
/* Style for visual feedback when code block is copied */
pre.copied::before {
  content: "✅"; /* Change icon to checkmark */
}
code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas,
    "Liberation Mono", "Courier New", monospace;
}

/*
Styles for prefers-color-scheme.
We'll default to dark-mode unless user has set a preference.
*/
body {
  background-color: var(--bg-color);
  color: var(--text-color);
}

output {
  background-color: var(--bg-color);
  border-color: var(--accent-color);
  color: var(--text-color);
}
/* Custmo scrollbar styles (ignored on iOS 14+) */
::-webkit-scrollbar {
  background-color: transparent;
}
::-webkit-scrollbar-thumb {
  background-color: transparent;
  border-radius: 1rem;
}
:hover::-webkit-scrollbar-thumb {
  background-color: var(--accent-color);
}

/* Allow the browser to style form elements light or dark as preferred by the user */
input,
textarea,
button,
select {
  /* Set color-scheme to dark by default, light if user has set a preference */
  color-scheme: light dark;
  /* Set accent colour to an intense bright purple */
  accent-color: var(--accent-color);
}
button {
  background-color: var(--accent-color);
  color: #fff;
}
button,
textarea {
  border-color: var(--accent-color);
}
.abort {
  accent-color: var(--abort-color);
  background-color: var(--abort-color);
  border-color: var(--abort-color);
}
/* Set flex-basis and line-height to avoid small shifts in the Send/Stop button */
button {
  flex-basis: 10ch;
  line-height: 1.5;
}
textarea {
  resize: vertical;
  /* Explicitly set font-size to (at least) 16px to prevent zooming on mobile */
  font-size: 16px;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg-color: #fff;
    --text-color: #000;
    --pre-hover: #0001;
  }
}
