/* ==========================================================================
   AI Chatbot — Global variables & reset
   ========================================================================== */

:root {
  --bg-primary: #0d0d0d;
  --bg-secondary: #171717;
  --bg-tertiary: #212121;
  --bg-hover: #2a2a2a;
  --bg-message-user: #2f2f2f;
  --bg-message-bot: transparent;
  --border: #333333;
  --text-primary: #ececec;
  --text-secondary: #a1a1a1;
  --text-muted: #6b6b6b;
  --accent: #b91c1c;
  --accent-hover: #991b1b;
  --accent-muted: rgba(185, 28, 28, 0.15);
  --danger: #ef4444;
  --danger-hover: #dc2626;
  --sidebar-width: 280px;
  --header-height: 56px;
  --input-area-max: 768px;
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --radius-xl: 24px;
  --shadow: 0 4px 24px rgba(0, 0, 0, 0.4);
  --transition: 0.2s cubic-bezier(0.4, 0, 0.2, 1);
  --font-sans: "Cairo", "Segoe UI", system-ui, -apple-system, sans-serif;
  --font-mono: "Cascadia Code", "Fira Code", Consolas, monospace;
}

*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
}

body {
  font-family: var(--font-sans);
  background: var(--bg-primary);
  color: var(--text-primary);
  line-height: 1.6;
  overflow: hidden;
  height: 100dvh;
}

button {
  font-family: inherit;
  cursor: pointer;
  border: none;
  background: none;
  color: inherit;
}

textarea {
  font-family: inherit;
  border: none;
  outline: none;
  resize: none;
  background: transparent;
  color: inherit;
}

a {
  color: var(--accent);
}

/* ==========================================================================
   App layout
   ========================================================================== */

.app {
  display: flex;
  height: 100dvh;
  width: 100%;
}

/* ==========================================================================
   Sidebar
   ========================================================================== */

.sidebar {
  width: var(--sidebar-width);
  min-width: var(--sidebar-width);
  background: var(--bg-secondary);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  z-index: 100;
  transition: transform var(--transition);
}

.sidebar-header {
  padding: 12px;
  display: flex;
  gap: 8px;
  align-items: center;
}

.btn-new-chat {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  font-size: 0.875rem;
  font-weight: 500;
  transition: background var(--transition), border-color var(--transition), transform 0.15s ease;
}

.btn-new-chat:hover {
  background: var(--bg-hover);
  border-color: var(--text-muted);
  transform: translateY(-1px);
}

.btn-new-chat:active {
  transform: translateY(0);
}

.sidebar-close {
  display: none;
}

.chat-history {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.history-item {
  display: flex;
  align-items: center;
  gap: 4px;
  border-radius: var(--radius-sm);
  transition: background var(--transition);
}

.history-item:hover {
  background: var(--bg-hover);
}

.history-item.active {
  background: var(--bg-tertiary);
}

.history-btn {
  flex: 1;
  text-align: right;
  padding: 10px 12px;
  font-size: 0.875rem;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  border-radius: var(--radius-sm);
  transition: color var(--transition);
}

.history-item:hover .history-btn,
.history-item.active .history-btn {
  color: var(--text-primary);
}

.history-delete {
  opacity: 0;
  padding: 8px;
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  transition: opacity var(--transition), color var(--transition), background var(--transition);
}

.history-item:hover .history-delete {
  opacity: 1;
}

.history-delete:hover {
  color: var(--danger);
  background: rgba(239, 68, 68, 0.1);
}

.sidebar-footer {
  padding: 12px;
  border-top: 1px solid var(--border);
}

.btn-clear-all {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 10px;
  font-size: 0.8125rem;
  color: var(--text-secondary);
  border-radius: var(--radius-sm);
  transition: color var(--transition), background var(--transition);
}

.btn-clear-all:hover {
  color: var(--danger);
  background: rgba(239, 68, 68, 0.08);
}

.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 90;
  opacity: 0;
  transition: opacity var(--transition);
}

.sidebar-overlay.visible {
  opacity: 1;
}

/* ==========================================================================
   Main chat panel
   ========================================================================== */

.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--bg-primary);
}

.chat-header {
  height: var(--header-height);
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.btn-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  transition: background var(--transition), color var(--transition), transform 0.15s ease;
}

.btn-icon:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
  transform: scale(1.05);
}

#btnOpenSidebar {
  display: none;
}

.chat-title {
  font-size: 1rem;
  font-weight: 600;
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ==========================================================================
   Chat container & messages
   ========================================================================== */

.chat-container {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  scroll-behavior: smooth;
  position: relative;
}

.welcome {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 48px 24px;
  min-height: 60%;
  animation: fadeIn 0.5s ease;
}

.welcome.hidden {
  display: none;
}

.welcome-icon {
  width: 72px;
  height: 72px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--accent-muted);
  border-radius: 50%;
  color: var(--accent);
  margin-bottom: 20px;
}

.welcome h2 {
  font-size: 1.5rem;
  font-weight: 600;
  margin-bottom: 8px;
}

.welcome p {
  color: var(--text-secondary);
  font-size: 0.9375rem;
  max-width: 400px;
}

.messages {
  max-width: var(--input-area-max);
  margin: 0 auto;
  padding: 24px 16px 16px;
  width: 100%;
}

/* Individual message row */
.message {
  display: flex;
  gap: 16px;
  padding: 20px 0;
  animation: slideUp 0.35s ease;
  position: relative;
}

.message + .message {
  border-top: 1px solid transparent;
}

.message.user {
  background: transparent;
}

.message.bot {
  background: transparent;
}

.message-avatar {
  width: 32px;
  height: 32px;
  min-width: 32px;
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.user-avatar {
  background: #5436da;
  color: #fff;
}

.bot-avatar {
  background: var(--accent);
  color: #fff;
  overflow: hidden;
}

.bot-avatar img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  padding: 2px;
}

.message-body {
  flex: 1;
  min-width: 0;
}

.message-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
}

.message-role {
  font-size: 0.875rem;
  font-weight: 600;
}

.message-time {
  font-size: 0.75rem;
  color: var(--text-muted);
}

.message-content {
  font-size: 0.9375rem;
  line-height: 1.7;
  word-wrap: break-word;
  overflow-wrap: break-word;
}

.message-content p {
  margin-bottom: 0.75em;
}

.message-content p:last-child {
  margin-bottom: 0;
}

.message-content pre {
  background: var(--bg-tertiary);
  border-radius: var(--radius-sm);
  padding: 14px;
  overflow-x: auto;
  margin: 12px 0;
  font-family: var(--font-mono);
  font-size: 0.8125rem;
}

.message-content code {
  font-family: var(--font-mono);
  font-size: 0.875em;
  background: var(--bg-tertiary);
  padding: 2px 6px;
  border-radius: 4px;
}

.message-content pre code {
  background: none;
  padding: 0;
}

.message-content ul,
.message-content ol {
  margin: 0.5em 0 0.75em 1.25em;
}

.message-content blockquote {
  border-right: 3px solid var(--accent);
  border-left: none;
  padding-right: 12px;
  padding-left: 0;
  color: var(--text-secondary);
  margin: 12px 0;
}

.message-actions {
  display: flex;
  gap: 4px;
  margin-top: 8px;
  opacity: 0;
  transition: opacity var(--transition);
}

.message:hover .message-actions {
  opacity: 1;
}

.btn-copy {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px 10px;
  font-size: 0.75rem;
  color: var(--text-muted);
  border-radius: var(--radius-sm);
  transition: background var(--transition), color var(--transition);
}

.btn-copy:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.btn-copy.copied {
  color: var(--accent);
}

/* ==========================================================================
   Typing indicator & loading bar
   ========================================================================== */

.typing-indicator {
  display: flex;
  align-items: center;
  gap: 16px;
  max-width: var(--input-area-max);
  margin: 0 auto;
  padding: 8px 16px 16px;
  animation: fadeIn 0.3s ease;
}

.typing-indicator.hidden {
  display: none;
}

.typing-dots {
  display: flex;
  gap: 5px;
  padding: 12px 16px;
  background: var(--bg-tertiary);
  border-radius: var(--radius-lg);
}

.typing-dots span {
  width: 8px;
  height: 8px;
  background: var(--text-muted);
  border-radius: 50%;
  animation: bounce 1.4s infinite ease-in-out both;
}

.typing-dots span:nth-child(1) { animation-delay: -0.32s; }
.typing-dots span:nth-child(2) { animation-delay: -0.16s; }

.loading-bar {
  position: sticky;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: var(--bg-tertiary);
  z-index: 10;
  overflow: hidden;
}

.loading-bar.hidden {
  display: none;
}

.loading-bar-inner {
  height: 100%;
  width: 30%;
  background: linear-gradient(90deg, var(--accent), #34d399);
  border-radius: 2px;
  animation: loadingSlide 1.2s ease-in-out infinite;
}

/* ==========================================================================
   Input area
   ========================================================================== */

.input-area {
  flex-shrink: 0;
  padding: 12px 16px 20px;
  background: linear-gradient(transparent, var(--bg-primary) 20%);
}

.input-form {
  max-width: var(--input-area-max);
  margin: 0 auto;
}

.input-wrapper {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius-xl);
  padding: 10px 10px 10px 18px;
  transition: border-color var(--transition), box-shadow var(--transition);
}

.input-wrapper:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent-muted);
}

.message-input {
  flex: 1;
  max-height: 200px;
  font-size: 0.9375rem;
  line-height: 1.5;
  padding: 6px 0;
}

.message-input::placeholder {
  color: var(--text-muted);
}

.btn-send {
  width: 40px;
  height: 40px;
  min-width: 40px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background var(--transition), transform 0.15s ease, opacity var(--transition);
}

.btn-send:hover:not(:disabled) {
  background: var(--accent-hover);
  transform: scale(1.06);
}

.btn-send:active:not(:disabled) {
  transform: scale(0.96);
}

.btn-send:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.btn-send .icon-send.hidden {
  display: none;
}

.spinner {
  width: 18px;
  height: 18px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

.spinner.hidden {
  display: none;
}

.input-hint {
  text-align: center;
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: 10px;
}

/* ==========================================================================
   Animations
   ========================================================================== */

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(12px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes bounce {
  0%, 80%, 100% { transform: scale(0.6); opacity: 0.5; }
  40% { transform: scale(1); opacity: 1; }
}

@keyframes loadingSlide {
  0% { transform: translateX(-100%); }
  100% { transform: translateX(400%); }
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ==========================================================================
   Scrollbar
   ========================================================================== */

.chat-container::-webkit-scrollbar,
.chat-history::-webkit-scrollbar {
  width: 6px;
}

.chat-container::-webkit-scrollbar-thumb,
.chat-history::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 3px;
}

/* ==========================================================================
   Responsive — mobile
   ========================================================================== */

@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    right: 0;
    left: auto;
    top: 0;
    bottom: 0;
    transform: translateX(100%);
    box-shadow: var(--shadow);
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .sidebar-close {
    display: flex;
  }

  .sidebar-overlay {
    display: block;
    pointer-events: none;
  }

  .sidebar-overlay.visible {
    pointer-events: auto;
  }

  #btnOpenSidebar {
    display: flex;
  }

  .message {
    gap: 12px;
    padding: 16px 0;
  }

  .message-actions {
    opacity: 1;
  }

  .welcome h2 {
    font-size: 1.25rem;
  }
}

@media (min-width: 769px) {
  .sidebar-overlay {
    display: none !important;
  }
}
