@import url('https://fonts.googleapis.com/css2?family=Caveat:wght@400;700&display=swap');

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

:root {
  --bg-primary: #fffef9;
  --bg-secondary: #f5f3f0;
  --text-primary: #2d2d2d;
  --text-secondary: #6b7280;
  --accent: #d4a574;
  --accent-light: #e8d4c0;
  --bubble-user: #d4a574;
  --bubble-assistant: #f0ebe4;
  --border: #e8dfd6;
}

:root[data-theme="dark"] {
  --bg-primary: #1a1a1a;
  --bg-secondary: #2d2d2d;
  --text-primary: #e8e8e8;
  --text-secondary: #a0a0a0;
  --accent: #c09860;
  --accent-light: #8b7355;
  --bubble-user: #8b7355;
  --bubble-assistant: #2d2d2d;
  --border: #3d3d3d;
}

body {
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  background: linear-gradient(135deg, var(--bg-primary) 0%, var(--bg-secondary) 100%);
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-primary);
  transition: background 0.3s ease;
}

.login-container {
  background: var(--bg-primary);
  border: 2px solid var(--border);
  border-radius: 15px;
  padding: 40px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.15);
  max-width: 400px;
  width: 100%;
  animation: slideUp 0.5s ease;
  position: relative;
}

.login-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image:
    repeating-linear-gradient(
      0deg,
      transparent,
      transparent 24px,
      var(--border) 24px,
      var(--border) 25px
    );
  pointer-events: none;
  border-radius: 15px;
  opacity: 0.3;
}

.login-container h1 {
  text-align: center;
  color: var(--accent);
  margin-bottom: 30px;
  font-size: 2.2em;
  font-family: 'Caveat', cursive;
  font-weight: 700;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  color: var(--text-secondary);
  margin-bottom: 8px;
  font-weight: 500;
}

.form-group input {
  width: 100%;
  padding: 12px;
  border: 2px solid var(--border);
  border-radius: 10px;
  font-size: 16px;
  background: var(--bg-secondary);
  color: var(--text-primary);
  transition: border-color 0.3s;
  font-family: 'Caveat', cursive;
  font-size: 18px;
}

.form-group input:focus {
  outline: none;
  border-color: var(--accent);
}

.login-btn {
  width: 100%;
  padding: 12px;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 10px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.2s, box-shadow 0.2s;
}

.login-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 20px rgba(212, 165, 116, 0.4);
}

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

.error {
  color: #d97706;
  font-size: 14px;
  margin-top: 10px;
  text-align: center;
}

.chat-layout {
  display: flex;
  height: 95vh;
  max-width: 95vw;
  margin: 10px;
  gap: 10px;
}

.chat-sidebar {
  width: 200px;
  background: var(--bg-secondary);
  border: 2px solid var(--border);
  border-radius: 15px;
  padding: 15px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

.sidebar-header {
  margin-bottom: 15px;
}

.new-chat-btn {
  width: 100%;
  padding: 10px;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 600;
  transition: background 0.3s;
}

.new-chat-btn:hover {
  opacity: 0.9;
}

.conversations-list {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.conversation-item {
  padding: 10px;
  background: var(--bg-primary);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.2s;
  display: flex;
  justify-content: space-between;
  align-items: center;
  overflow: hidden;
}

.conversation-item:hover {
  background: var(--accent-light);
}

.conversation-item.active {
  background: var(--accent);
  color: white;
}

.conversation-text {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: 12px;
}

.delete-conv-btn {
  background: none;
  border: none;
  color: inherit;
  cursor: pointer;
  font-size: 16px;
  padding: 0 5px;
}

.chat-main {
  flex: 1;
}

.chat-container {
  background: var(--bg-primary);
  border: 2px solid var(--border);
  border-radius: 15px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.15);
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  animation: slideUp 0.5s ease;
  position: relative;
  overflow: hidden;
}

.chat-header {
  background: var(--accent);
  color: white;
  padding: 20px;
  border-bottom: 2px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.chat-header h1 {
  font-size: 1.8em;
  font-family: 'Caveat', cursive;
  font-weight: 700;
}

.header-actions {
  display: flex;
  gap: 10px;
  align-items: center;
}

.theme-toggle,
.clear-btn {
  background: rgba(255, 255, 255, 0.2);
  border: none;
  color: white;
  padding: 8px 12px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 18px;
  transition: background 0.3s;
  display: flex;
  align-items: center;
}

.theme-toggle:hover,
.clear-btn:hover {
  background: rgba(255, 255, 255, 0.3);
}

.logout-btn {
  background: rgba(255, 255, 255, 0.2);
  border: none;
  color: white;
  padding: 8px 16px;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 600;
  transition: background 0.3s;
}

.logout-btn:hover {
  background: rgba(255, 255, 255, 0.3);
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 15px;
  background-image:
    repeating-linear-gradient(
      0deg,
      transparent,
      transparent 24px,
      var(--border) 24px,
      var(--border) 25px
    );
  background-position: 0 20px;
}

.message {
  display: flex;
  animation: fadeIn 0.3s ease;
}

.message.user {
  justify-content: flex-end;
}

.message-bubble {
  max-width: 70%;
  padding: 12px 16px;
  border-radius: 12px;
  word-wrap: break-word;
  line-height: 1.6;
  font-family: 'Caveat', cursive;
  font-size: 18px;
}

.message.user .message-bubble {
  background: var(--bubble-user);
  color: white;
  border-bottom-right-radius: 4px;
}

.message.assistant .message-bubble {
  background: var(--bubble-assistant);
  color: var(--text-primary);
  border-bottom-left-radius: 4px;
}

.chat-input-area {
  padding: 20px;
  border-top: 2px solid var(--border);
  display: flex;
  gap: 10px;
  background: var(--bg-secondary);
  align-items: center;
}

.file-btn {
  background: var(--accent);
  color: white;
  border: none;
  padding: 12px 15px;
  border-radius: 10px;
  cursor: pointer;
  font-size: 18px;
  transition: opacity 0.3s;
}

.file-btn:hover {
  opacity: 0.9;
}

.chat-input-area input {
  flex: 1;
  padding: 12px;
  border: 2px solid var(--border);
  border-radius: 10px;
  background: var(--bg-primary);
  color: var(--text-primary);
  font-size: 16px;
  font-family: 'Caveat', cursive;
  font-size: 18px;
  transition: border-color 0.3s;
}

.chat-input-area input:focus {
  outline: none;
  border-color: var(--accent);
}

.send-btn {
  background: var(--accent);
  color: white;
  border: none;
  padding: 12px 20px;
  border-radius: 10px;
  cursor: pointer;
  font-weight: 600;
  transition: transform 0.2s, box-shadow 0.2s;
}

.send-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 5px 20px rgba(212, 165, 116, 0.4);
}

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

.search-toggle {
  opacity: 0.5;
  transition: opacity 0.3s;
}

.search-toggle.active {
  opacity: 1;
}

.tabs {
  display: flex;
  gap: 0;
  border-bottom: 2px solid var(--border);
  padding: 0;
}

.tab-btn {
  flex: 1;
  padding: 12px;
  background: var(--bg-secondary);
  border: none;
  cursor: pointer;
  font-weight: 600;
  color: var(--text-secondary);
  transition: all 0.3s;
  border-bottom: 3px solid transparent;
}

.tab-btn:hover {
  background: var(--bg-primary);
}

.tab-btn.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
  background: var(--bg-primary);
}

.tab-content {
  display: none;
  flex: 1;
  overflow-y: auto;
  padding: 20px;
}

.tab-content.active {
  display: flex;
  flex-direction: column;
}

.instructions-content {
  display: flex;
  flex-direction: column;
  gap: 15px;
  height: 100%;
}

.instructions-content h2 {
  color: var(--accent);
  margin: 0;
  font-family: 'Caveat', cursive;
  font-size: 1.8em;
}

.instructions-textarea {
  flex: 1;
  padding: 15px;
  border: 2px solid var(--border);
  border-radius: 10px;
  background: var(--bg-secondary);
  color: var(--text-primary);
  font-family: 'Caveat', cursive;
  font-size: 16px;
  resize: none;
}

.instructions-textarea:focus {
  outline: none;
  border-color: var(--accent);
}

.save-btn,
.reset-btn {
  padding: 10px 20px;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 600;
  transition: opacity 0.3s;
}

.save-btn:hover,
.reset-btn:hover {
  opacity: 0.9;
}

.reset-btn {
  background: var(--text-secondary);
}

.memories-content {
  display: flex;
  flex-direction: column;
  gap: 15px;
  height: 100%;
}

.memories-content h2 {
  color: var(--accent);
  margin: 0;
  font-family: 'Caveat', cursive;
  font-size: 1.8em;
}

.memories-list {
  flex: 1;
  overflow-y: auto;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 12px;
  padding-right: 10px;
}

.empty-memories {
  grid-column: 1 / -1;
  text-align: center;
  color: var(--text-secondary);
  padding: 20px;
}

.memory-card {
  background: var(--bg-secondary);
  border: 2px solid var(--border);
  border-radius: 10px;
  padding: 15px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  transition: transform 0.2s, box-shadow 0.2s;
}

.memory-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(212, 165, 116, 0.2);
}

.memory-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 10px;
}

.memory-header h3 {
  margin: 0;
  color: var(--accent);
  font-family: 'Caveat', cursive;
  font-size: 1.3em;
  flex: 1;
  word-break: break-word;
}

.delete-memory-btn {
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 18px;
  transition: color 0.2s;
  flex-shrink: 0;
}

.delete-memory-btn:hover {
  color: #ef4444;
}

.memory-content {
  margin: 0;
  color: var(--text-primary);
  font-size: 14px;
  line-height: 1.5;
  word-break: break-word;
}

.memory-timestamp {
  color: var(--text-secondary);
  font-size: 12px;
}

.memory-input-area {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding-top: 15px;
  border-top: 2px solid var(--border);
}

.memory-input {
  padding: 12px;
  border: 2px solid var(--border);
  border-radius: 8px;
  background: var(--bg-secondary);
  color: var(--text-primary);
  font-family: 'Caveat', cursive;
  font-size: 16px;
}

.memory-input:focus {
  outline: none;
  border-color: var(--accent);
}

.memory-textarea {
  padding: 12px;
  border: 2px solid var(--border);
  border-radius: 8px;
  background: var(--bg-secondary);
  color: var(--text-primary);
  font-family: 'Caveat', cursive;
  font-size: 16px;
  resize: vertical;
  min-height: 80px;
  max-height: 200px;
}

.memory-textarea:focus {
  outline: none;
  border-color: var(--accent);
}

.add-memory-btn {
  padding: 12px;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 600;
  transition: opacity 0.3s;
}

.add-memory-btn:hover {
  opacity: 0.9;
}

.typing-indicator {
  display: flex;
  gap: 4px;
  padding: 10px;
}

.typing-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
  animation: bounce 1.4s infinite;
}

.typing-dot:nth-child(2) {
  animation-delay: 0.2s;
}

.typing-dot:nth-child(3) {
  animation-delay: 0.4s;
}

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

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

@keyframes bounce {
  0%, 60%, 100% {
    opacity: 1;
    transform: translateY(0);
  }
  30% {
    opacity: 0.7;
    transform: translateY(-10px);
  }
}

/* Markdown Styling */
.message-bubble h1,
.message-bubble h2,
.message-bubble h3,
.message-bubble h4,
.message-bubble h5,
.message-bubble h6 {
  margin: 12px 0 8px 0;
  font-weight: 700;
}

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

.message-bubble p {
  margin: 8px 0;
}

.message-bubble ul,
.message-bubble ol {
  margin: 8px 0 8px 20px;
  padding-left: 0;
}

.message-bubble li {
  margin: 4px 0;
}

.message-bubble code {
  background: rgba(0, 0, 0, 0.1);
  padding: 2px 6px;
  border-radius: 4px;
  font-family: 'Courier New', monospace;
  font-size: 0.9em;
}

.message.assistant .message-bubble code {
  background: rgba(0, 0, 0, 0.15);
}

.message.user .message-bubble code {
  background: rgba(255, 255, 255, 0.2);
}

.message-bubble pre {
  background: rgba(0, 0, 0, 0.05);
  border-left: 3px solid var(--accent);
  padding: 10px;
  border-radius: 4px;
  overflow-x: auto;
  margin: 8px 0;
  font-family: 'Courier New', monospace;
  font-size: 0.85em;
  line-height: 1.4;
}

.message.assistant .message-bubble pre {
  background: rgba(0, 0, 0, 0.08);
}

.message-bubble blockquote {
  border-left: 3px solid var(--accent);
  padding-left: 12px;
  margin-left: 0;
  margin-right: 0;
  opacity: 0.8;
  font-style: italic;
}

.message-bubble strong {
  font-weight: 700;
}

.message-bubble em {
  font-style: italic;
}

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

.message.assistant .message-bubble a {
  color: #b8860b;
}

.message-bubble hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 12px 0;
}

.files-container {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
}

.file-item {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.file-header {
  display: flex;
  gap: 8px;
}

.file-download-btn {
  flex: 1;
  padding: 10px 15px;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 600;
  transition: opacity 0.3s;
  text-align: left;
}

.file-download-btn:hover {
  opacity: 0.9;
}

.message.assistant .file-download-btn {
  background: #b8860b;
}

.preview-btn {
  padding: 10px 12px;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 16px;
  transition: opacity 0.3s;
}

.preview-btn:hover {
  opacity: 0.9;
}

.message.assistant .preview-btn {
  background: #b8860b;
}

.file-preview {
  background: rgba(0, 0, 0, 0.05);
  border-left: 3px solid var(--accent);
  padding: 10px;
  border-radius: 4px;
  overflow-x: auto;
  max-height: 300px;
  overflow-y: auto;
}

.file-preview-content {
  width: 100%;
}

.file-preview pre {
  margin: 0;
  font-family: 'Courier New', monospace;
  font-size: 12px;
  line-height: 1.4;
  color: var(--text-primary);
  background: rgba(0, 0, 0, 0.05);
  padding: 10px;
  border-radius: 4px;
  overflow-x: auto;
}

.file-preview code {
  background: transparent;
  padding: 0;
  border-radius: 0;
  font-family: 'Courier New', monospace;
}

.file-preview img {
  max-width: 100%;
  border-radius: 4px;
  display: block;
}

.preview-iframe {
  width: 100%;
  height: 400px;
  border: 1px solid var(--border);
  border-radius: 4px;
}

.message.assistant .file-preview {
  background: rgba(0, 0, 0, 0.08);
}
