/**
 * Chat Interface
 * Full-featured chat with sidebar, markdown, and conversation history
 * Credits: Luke Steuber
 */

/* ========== Reset & Base ========== */
*, *::before, *::after { box-sizing: border-box; }

body {
  margin: 0;
  padding: 0;
  font-family: var(--font-sans);
  background: var(--bg-0);
  color: var(--text-0);
  overflow: hidden;
  height: 100vh;
}

.hidden { display: none !important; }

/* ========== App Layout ========== */
.chat-app {
  display: flex;
  height: 100vh;
  width: 100%;
}

/* ========== Sidebar ========== */
.sidebar {
  width: 280px;
  min-width: 280px;
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  background: var(--bg-1);
  overflow: hidden;
}

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

.sidebar-header h2 {
  margin: 0;
  font-size: 1.25rem;
  letter-spacing: -0.02em;
}

.sidebar-new {
  padding: 4px 8px;
  font-size: 0.85rem;
}

.sidebar-section {
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
}

.sidebar-label {
  display: block;
  font-size: 0.75rem;
  letter-spacing: 0.02em;
  color: var(--text-2);
  margin-bottom: 4px;
  font-weight: 600;
}

.sidebar-section select,
.sidebar-section textarea {
  width: 100%;
  font-size: 0.85rem;
}

.system-prompt-input {
  resize: vertical;
  min-height: 60px;
  max-height: 120px;
  font-family: var(--font-sans);
  font-size: 0.85rem !important;
  line-height: 1.4;
}

/* History list */
.history-list {
  max-height: 200px;
  overflow-y: auto;
}

.history-empty {
  font-size: 0.75rem;
  text-align: center;
  padding: 8px 0;
  margin: 0;
}

.history-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 4px 4px;
  cursor: pointer;
  border: 1px solid transparent;
  font-size: 0.85rem;
  transition: border-color 0.1s;
}

.history-item:hover {
  border-color: var(--text-0);
}

.history-item.active {
  border-left: 1px solid var(--accent);
  background: var(--bg-0);
}

.history-item-title {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.history-item-delete {
  background: none;
  border: none;
  color: var(--text-2);
  cursor: pointer;
  padding: 2px 6px;
  font-size: 0.75rem;
  opacity: 0;
  transition: opacity 0.1s;
}

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

.history-item-delete:hover {
  color: var(--accent);
}

.sidebar-footer {
  margin-top: auto;
  padding: 8px 12px;
  border-top: 1px solid var(--border);
  display: flex;
  gap: 4px;
}

.sidebar-action {
  flex: 1;
  text-align: center;
  font-size: 0.75rem;
  padding: 4px 4px;
  text-decoration: none;
}

/* ========== Main Chat Area ========== */
.chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  position: relative;
}

/* Mobile header (hidden on desktop) */
.mobile-header {
  display: none;
  padding: 8px 12px;
  border-bottom: 1px solid var(--text-0);
  align-items: center;
  justify-content: space-between;
}

.mobile-title {
  font-size: 1.1rem;
  font-weight: 600;
  letter-spacing: -0.02em;
}

/* ========== Messages ========== */
.messages-container {
  flex: 1;
  overflow-y: auto;
  padding: 16px 24px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  scroll-behavior: smooth;
}

/* Welcome screen */
.welcome-message {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}

.welcome-inner {
  text-align: center;
  max-width: 500px;
  padding: 32px;
}

.welcome-inner h1 {
  font-size: 3rem;
  letter-spacing: -0.02em;
  margin: 0 0 8px 0;
}

.welcome-inner p {
  color: var(--text-2);
  font-size: 0.95rem;
  margin: 0 0 24px 0;
}

.welcome-hints {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
}

.hint-chip {
  background: var(--bg-1);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text-0);
  padding: 4px 12px;
  font-size: 0.85rem;
  font-family: var(--font-sans);
  cursor: pointer;
  transition: all var(--ease);
}

.hint-chip:hover {
  border-color: var(--accent);
  color: var(--accent);
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

/* Message bubbles */
.message {
  max-width: 800px;
  width: 100%;
  margin: 0 auto;
  padding: 0;
}

.message-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 4px;
}

.message-role {
  font-size: 0.75rem;
  letter-spacing: 0.02em;
  color: var(--text-2);
  font-weight: 600;
}

.message.user .message-role {
  color: var(--text-0);
}

.message.assistant .message-role {
  color: var(--accent);
}

.message-actions {
  display: flex;
  gap: 4px;
  opacity: 0;
  transition: opacity 0.15s;
}

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

.msg-action-btn {
  background: none;
  border: 1px solid transparent;
  border-radius: 4px;
  color: var(--text-2);
  cursor: pointer;
  padding: 2px 6px;
  font-size: 0.75rem;
  font-family: var(--font-sans);
  letter-spacing: 0.02em;
}

.msg-action-btn:hover {
  color: var(--text-0);
  border-color: var(--border);
}

/* Message content - markdown rendered */
.message-content {
  font-size: 0.95rem;
  line-height: 1.7;
  word-wrap: break-word;
  overflow-wrap: break-word;
}

.message.user .message-content {
  white-space: pre-wrap;
}

/* Markdown styles inside messages */
.message-content h1,
.message-content h2,
.message-content h3,
.message-content h4 {
  margin: 1em 0 0.5em 0;
  font-weight: 600;
  letter-spacing: -0.02em;
}

.message-content h1 { font-size: 1.5em; }
.message-content h2 { font-size: 1.3em; }
.message-content h3 { font-size: 1.1em; }

.message-content p {
  margin: 0 0 0.8em 0;
}

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

.message-content ul,
.message-content ol {
  margin: 0.5em 0;
  padding-left: 1.5em;
}

.message-content li {
  margin: 0.3em 0;
}

.message-content blockquote {
  border-left: 1px solid var(--accent);
  margin: 0.8em 0;
  padding: 4px 12px;
  color: var(--text-2);
}

.message-content a {
  color: var(--accent);
  text-decoration: underline;
}

/* Code blocks */
.message-content pre {
  background: #1a1a2e;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 12px;
  overflow-x: auto;
  font-family: var(--font-mono);
  font-size: 0.85rem;
  line-height: 1.5;
  margin: 0.8em 0;
  position: relative;
}

.message-content pre code {
  background: none;
  padding: 0;
  font-size: inherit;
  color: #e0e0e0;
}

.message-content code {
  font-family: var(--font-mono);
  font-size: 0.9em;
  background: rgba(128,128,128,0.15);
  padding: 2px 6px;
  border-radius: 3px;
}

.code-copy-btn {
  position: absolute;
  top: 4px;
  right: 4px;
  background: rgba(255,255,255,0.1);
  border: 1px solid rgba(255,255,255,0.2);
  border-radius: 4px;
  color: rgba(255,255,255,0.6);
  cursor: pointer;
  padding: 4px 8px;
  font-size: 11px;
  font-family: var(--font-sans);
  letter-spacing: 0.05em;
}

.code-copy-btn:hover {
  background: rgba(255,255,255,0.2);
  color: #fff;
}

/* Table styling */
.message-content table {
  border-collapse: collapse;
  width: 100%;
  margin: 0.8em 0;
  font-size: 0.85rem;
}

.message-content th,
.message-content td {
  border: 1px solid var(--border);
  padding: 4px 8px;
  text-align: left;
}

.message-content th {
  font-weight: 600;
  font-size: 0.75rem;
  letter-spacing: 0.02em;
}

/* Loading indicator */
.message.loading .message-content::after {
  content: '';
  display: inline-block;
  width: 6px;
  height: 16px;
  background: var(--accent);
  margin-left: 2px;
  animation: blink 0.8s infinite;
  vertical-align: text-bottom;
}

@keyframes blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0; }
}

/* Error messages */
.message.error .message-content {
  color: var(--accent);
}

/* ========== Input Area ========== */
.input-area {
  padding: 12px 24px;
  border-top: 1px solid var(--border);
  background: var(--bg-0);
}

.input-form {
  max-width: 800px;
  margin: 0 auto;
}

.input-wrapper {
  display: flex;
  gap: 8px;
  align-items: flex-end;
  border: 1px solid var(--text-0);
  border-radius: 8px;
  background: var(--bg-1);
  padding: 4px 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

.input-wrapper:focus-within {
  border-color: var(--accent);
}

.message-input {
  flex: 1;
  border: none;
  background: transparent;
  color: var(--text-0);
  font-family: var(--font-sans);
  font-size: 0.95rem;
  line-height: 1.5;
  resize: none;
  min-height: 24px;
  max-height: 200px;
  padding: 4px 4px;
  outline: none;
}

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

.send-btn {
  background: var(--accent);
  border: none;
  border-radius: 6px;
  color: white;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: all var(--ease);
}

.send-btn:hover {
  opacity: 0.85;
  transform: translateY(-1px);
}

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

.input-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 4px;
  padding: 0 4px;
}

.provider-badge {
  font-size: 11px;
  letter-spacing: 0.02em;
  color: var(--text-2);
}

.stop-btn {
  background: none;
  border: 1px solid var(--accent);
  border-radius: 4px;
  color: var(--accent);
  padding: 4px 12px;
  font-size: 0.75rem;
  font-family: var(--font-sans);
  letter-spacing: 0.02em;
  cursor: pointer;
  transition: all var(--ease);
}

.stop-btn:hover {
  background: var(--accent);
  color: white;
}

/* ========== Toast ========== */
.toast {
  position: fixed;
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text-0);
  color: var(--bg-0);
  padding: 8px 16px;
  border: 1px solid var(--bg-0);
  border-radius: 6px;
  font-size: 0.85rem;
  letter-spacing: 0.02em;
  z-index: 2000;
  opacity: 0;
  pointer-events: none;
  transition: opacity 200ms ease;
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}

.toast.show { opacity: 1; }

/* ========== Responsive ========== */
@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    left: -300px;
    top: 0;
    bottom: 0;
    z-index: 100;
    transition: left 0.2s ease;
  }

  .sidebar.open {
    left: 0;
  }

  .mobile-header {
    display: flex;
  }

  .messages-container {
    padding: 12px;
  }

  .input-area {
    padding: 8px 12px;
  }

  .welcome-inner h1 {
    font-size: 2rem;
  }

  .message-actions {
    opacity: 1;
  }
}

/* Overlay for mobile sidebar */
.sidebar-overlay {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,0.5);
  z-index: 99;
}

.sidebar-overlay.visible {
  display: block;
}

/* ========== Scrollbar ========== */
.messages-container::-webkit-scrollbar {
  width: 6px;
}

.messages-container::-webkit-scrollbar-track {
  background: transparent;
}

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

.messages-container::-webkit-scrollbar-thumb:hover {
  background: var(--text-2);
}
