:root {
  --background-hsl: 220 20% 12%; /* Deep, cool blue-black */
  --foreground-hsl: 220 10% 95%;
  --card-hsl: 220 20% 15%;
  --card-foreground-hsl: 220 10% 90%;
  --popover-hsl: 220 20% 10%;
  --popover-foreground-hsl: 220 10% 95%;
  --primary-hsl: 158 80% 60%; /* Vibrant mint green */
  --primary-foreground-hsl: 220 20% 10%;
  --secondary-hsl: 220 15% 30%;
  --secondary-foreground-hsl: 220 10% 95%;
  --muted-hsl: 220 15% 25%;
  --muted-foreground-hsl: 220 10% 60%;
  --accent-hsl: 220 15% 40%;
  --accent-foreground-hsl: 220 10% 95%;
  --border-hsl: 220 15% 25%;
  --input-hsl: 220 15% 20%;
  --ring-hsl: 158 80% 70%;

  --font-sans: 'Tajawal', sans-serif;
}

body {
  font-family: var(--font-sans);
  background-color: hsl(var(--background-hsl));
  color: hsl(var(--foreground-hsl));
  overscroll-behavior: none;
}

.app-container {
  display: flex;
  flex-direction: row-reverse; /* Sidebar on the right for RTL */
  height: 100vh;
  width: 100vw;
  overflow: hidden;
}

.sidebar {
  width: 280px;
  background-color: hsl(var(--popover-hsl));
  border-left: 1px solid hsl(var(--border-hsl));
  display: flex;
  flex-direction: column;
  padding: 1rem;
  position: fixed; /* For mobile overlay */
  top: 0; right: 0; height: 100%;
  z-index: 100;
  transform: translateX(100%);
}

.sidebar-backdrop {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background-color: rgba(0,0,0,0.5);
  z-index: 99;
}

.sidebar-header {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    margin-bottom: 1.5rem;
    position: relative;
}

.sidebar-close-btn {
    display: none; /* Hidden by default, shown in media query */
    position: absolute;
    top: -4px;
    left: -4px;
    color: hsl(var(--muted-foreground-hsl));
    padding: 0.25rem;
    border-radius: 9999px;
    transition: background-color 0.2s, color 0.2s;
}
.sidebar-close-btn:hover {
    background-color: hsl(var(--muted-hsl));
    color: hsl(var(--foreground-hsl));
}

.new-chat-btn {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: 0.75rem 1rem;
  border: 1px solid hsl(var(--border-hsl));
  border-radius: 0.5rem;
  background-color: hsl(var(--card-hsl));
  color: hsl(var(--card-foreground-hsl));
  transition: background-color 0.2s;
}

.new-chat-btn:hover {
  background-color: hsl(var(--muted-hsl));
}

.chat-history {
  flex-grow: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.chat-link {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 0.75rem;
  padding: 0.6rem 0.75rem;
  border-radius: 0.5rem;
  color: hsl(var(--muted-foreground-hsl));
  text-decoration: none;
  transition: background-color 0.2s, color 0.2s;
  position: relative;
}

.chat-link:hover, .chat-link.active {
  background-color: hsl(var(--secondary-hsl));
  color: hsl(var(--secondary-foreground-hsl));
}

.chat-link .truncate {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.delete-chat-btn {
    color: hsl(var(--muted-foreground-hsl));
    border-radius: 0.375rem;
    padding: 0.25rem;
    opacity: 0;
    transition: opacity 0.2s, background-color 0.2s, color 0.2s;
}

.chat-link:hover .delete-chat-btn {
    opacity: 1;
}

.delete-chat-btn:hover {
    background-color: hsl(0 60% 50% / 0.2);
    color: hsl(0 80% 80%);
}

.main-content {
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  height: 100vh;
  position: relative;
}

.menu-btn {
    display: none; /* Hidden by default */
    position: fixed;
    top: 1rem;
    right: 1rem;
    z-index: 50;
    padding: 0.5rem;
    border-radius: 9999px;
    background-color: hsl(var(--card-hsl));
    color: hsl(var(--card-foreground-hsl));
    box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
}

.welcome-screen {
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    height: 100%; text-align: center; padding: 2rem;
}

.logo-container {
    padding: 1rem; background-color: hsl(var(--primary-hsl));
    border-radius: 9999px; margin-bottom: 2rem;
    color: hsl(var(--primary-foreground-hsl));
}

.welcome-title {
    font-size: 2.5rem; font-weight: 700; margin-bottom: 2rem; color: hsl(var(--foreground-hsl));
}
.welcome-title em {
    font-style: italic; color: hsl(var(--primary-hsl));
}

.start-chat-btn {
    display: inline-flex; align-items: center; gap: 0.75rem;
    background-color: hsl(var(--primary-hsl));
    color: hsl(var(--primary-foreground-hsl));
    padding: 0.75rem 1.5rem; border-radius: 0.5rem; font-weight: 500; transition: transform 0.2s;
}
.start-chat-btn:hover { transform: scale(1.05); }

.chat-screen {
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.messages-container {
  flex-grow: 1;
  overflow-y: auto;
  padding: 1rem 1rem 0 1rem;
}

.empty-chat-placeholder {
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    height: 100%; gap: 1rem;
}

.message {
  display: flex;
  gap: 1rem;
  padding: 1.25rem;
  margin: 0 -1rem;
}

.message.assistant {
  background-color: hsl(var(--card-hsl) / 0.5);
}

.message-icon {
  flex-shrink: 0;
  width: 2.5rem; height: 2.5rem;
  border-radius: 9999px;
  display: flex; align-items: center; justify-content: center;
  background-color: hsl(var(--secondary-hsl));
}
.message.user .message-icon {
    background-color: hsl(var(--primary-hsl));
    color: hsl(var(--primary-foreground-hsl));
}

.message-content {
  padding-top: 0.25rem;
  color: hsl(var(--card-foreground-hsl));
  max-width: 100%;
  overflow-x: auto;
}

.message-content p { margin-bottom: 1rem; }
.message-content h1, .message-content h2, .message-content h3 { font-weight: bold; margin-bottom: 0.5rem; margin-top: 1rem; }
.message-content ul, .message-content ol { margin-left: 1.5rem; margin-bottom: 1rem; list-style: revert; }
.message-content a { color: hsl(var(--primary-hsl)); text-decoration: underline; }

.message-content pre {
  background-color: hsl(var(--popover-hsl));
  border: 1px solid hsl(var(--border-hsl));
  border-radius: 0.5rem;
  padding: 1rem;
  margin: 1rem 0;
  white-space: pre-wrap;
  word-wrap: break-word;
  font-family: monospace;
  font-size: 0.9rem;
}

.message-content code:not(pre > code) {
    background-color: hsl(var(--muted-hsl));
    padding: 0.2rem 0.4rem;
    border-radius: 0.25rem;
    font-family: monospace;
    font-size: 0.9em;
}

.typing-dots {
    display: flex; gap: 0.25rem; align-items: center; padding: 0.5rem;
}
.typing-dots span {
    width: 8px; height: 8px; background-color: hsl(var(--muted-foreground-hsl));
    border-radius: 50%;
}

.input-area {
  padding: 1rem;
  background: linear-gradient(to top, hsl(var(--background-hsl)) 50%, transparent);
}

.input-form {
  position: relative;
}

.chat-input {
  width: 100%;
  background-color: hsl(var(--card-hsl));
  color: hsl(var(--card-foreground-hsl));
  border: 1px solid hsl(var(--border-hsl));
  border-radius: 0.75rem;
  padding: 1rem 3.5rem 1rem 1.5rem; /* RTL padding */
  resize: none;
  font-size: 1rem;
  line-height: 1.5;
  max-height: 200px;
  overflow-y: auto;
}
.chat-input:focus {
  outline: none;
  border-color: hsl(var(--ring-hsl));
  box-shadow: 0 0 0 2px hsl(var(--background-hsl)), 0 0 0 4px hsl(var(--ring-hsl));
}
.chat-input:disabled { background-color: hsl(var(--muted-hsl)); }

.send-btn {
  position: absolute;
  left: 0.75rem; /* RTL position */
  top: 50%;
  transform: translateY(-50%);
  width: 2.25rem; height: 2.25rem;
  background-color: hsl(var(--primary-hsl));
  color: hsl(var(--primary-foreground-hsl));
  border-radius: 0.5rem;
  display: flex; align-items: center; justify-content: center;
  transition: transform 0.2s;
}

.send-btn:hover { transform: translateY(-50%) scale(1.1); }
.send-btn:disabled { background-color: hsl(var(--muted-hsl)); color: hsl(var(--muted-foreground-hsl)); cursor: not-allowed; }

@media (max-width: 768px) {
  .sidebar {
    box-shadow: -5px 0 15px rgba(0,0,0,0.2);
  }
  .main-content {
    width: 100%;
  }
  .menu-btn {
    display: flex;
  }
  .sidebar-close-btn {
    display: block;
  }
  .message-content {
    overflow-x: hidden; /* Prevent horizontal scroll on mobile */
  }
  .message-content pre {
    white-space: pre-wrap;
    word-break: break-all;
  }
}

@media (min-width: 769px) {
  .sidebar {
    position: relative; /* Static on desktop */
    transform: translateX(0) !important; /* Always visible */
  }
  .sidebar-backdrop { display: none; }
}