/* Reset and Variables */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

:root {
  --bg: #f6f4ef;
  --text: #2e2e2e;
  --ai-bg: #ede7f6;
  --user-bg: #fff9c4;
  --container-bg: #ffffff;
  --accent: #5a4dc8;
  --border: #e0e0e0;
  --shadow: 0 2px 8px rgba(0,0,0,0.08);
  --radius: 18px;
  --transition: 0.2s ease;
}

[data-theme="dark"] {
  --bg: #0f0f0f;
  --text: #f0f0f0;
  --ai-bg: #1a1a2e;
  --user-bg: #16213e;
  --container-bg: #1a1a1a;
  --accent: #7c6ff3;
  --border: #2a2a2a;
  --shadow: 0 2px 12px rgba(0,0,0,0.5);
}

/* Base Styles */
html {
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
  height: 100%;
}

body {
  font-family: 'Crimson Text', Georgia, serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  height: 100%;
  overflow: hidden;
  transition: background-color var(--transition), color var(--transition);
}

/* App Container */
.app-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  max-width: 100%;
  background: var(--container-bg);
  position: relative;
}

/* Header */
.header {
  background: var(--container-bg);
  padding: 1rem;
  padding-top: max(1rem, env(safe-area-inset-top));
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.header h1 {
  font-size: 1.25rem;
  font-weight: 600;
  color: var(--accent);
}

.theme-toggle,
.memory-garden-btn {
  width: 40px;
  height: 40px;
  border: none;
  background: transparent;
  color: var(--text);
  cursor: pointer;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color var(--transition);
}

.theme-toggle:hover,
.memory-garden-btn:hover {
  background: var(--border);
}

.sun-icon,
.moon-icon {
  position: absolute;
  transition: opacity var(--transition), transform var(--transition);
}

[data-theme="dark"] .sun-icon {
  opacity: 0;
  transform: rotate(180deg);
}

[data-theme="dark"] .moon-icon {
  opacity: 1;
  transform: rotate(0);
}

.sun-icon {
  opacity: 1;
  transform: rotate(0);
}

.moon-icon {
  opacity: 0;
  transform: rotate(-180deg);
}

/* Chat Container */
.chat-container {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 1rem;
  -webkit-overflow-scrolling: touch;
  scroll-behavior: smooth;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

/* Messages */
.message {
  padding: 0.875rem 1.125rem;
  border-radius: var(--radius);
  max-width: 85%;
  word-wrap: break-word;
  animation: fadeIn 0.3s ease-out;
  font-size: 1rem;
  line-height: 1.5;
  position: relative;
}

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

.message.user {
  background: var(--user-bg);
  align-self: flex-end;
  border-bottom-right-radius: 4px;
  margin-left: auto;
}

.message.ainasse {
  background: var(--ai-bg);
  align-self: flex-start;
  border-bottom-left-radius: 4px;
  font-style: italic;
}

.message.typing::after {
  content: '...';
  animation: typing 1.4s infinite;
  margin-left: 2px;
}

@keyframes typing {
  0%, 60%, 100% { opacity: 0; }
  30% { opacity: 1; }
}

/* Input Container */
.input-container {
  background: var(--container-bg);
  border-top: 1px solid var(--border);
  padding: 1rem;
  padding-bottom: max(1rem, env(safe-area-inset-bottom));
  display: flex;
  gap: 0.75rem;
  align-items: flex-end;
}

.message-input {
  flex: 1;
  min-height: 44px;
  max-height: 120px;
  padding: 0.75rem 1rem;
  border: 1px solid var(--border);
  border-radius: 22px;
  background: var(--bg);
  color: var(--text);
  font-size: 16px; /* Prevents zoom on iOS */
  font-family: inherit;
  resize: none;
  outline: none;
  transition: border-color var(--transition);
  line-height: 1.4;
}

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

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

.send-button {
  width: 44px;
  height: 44px;
  min-width: 44px;
  border-radius: 50%;
  background: var(--accent);
  color: white;
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all var(--transition);
  flex-shrink: 0;
}

.send-button:hover:not(:disabled) {
  transform: scale(1.05);
}

.send-button:active:not(:disabled) {
  transform: scale(0.95);
}

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

.send-button svg {
  width: 20px;
  height: 20px;
}

/* Status Bar */
.status-bar {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background: var(--accent);
  color: white;
  padding: 0.5rem 1rem;
  text-align: center;
  font-size: 0.875rem;
  transform: translateY(100%);
  transition: transform var(--transition);
}

.status-bar.show {
  transform: translateY(0);
}

/* Desktop Styles */
@media (min-width: 768px) {
  .app-container {
    max-width: 600px;
    margin: 2vh auto;
    height: 96vh;
    border-radius: 12px;
    box-shadow: var(--shadow);
    overflow: hidden;
  }

  .header {
    border-radius: 12px 12px 0 0;
  }

  .message {
    max-width: 70%;
  }

  .chat-container {
    padding: 1.5rem;
  }
}

/* Loading Animation */
.loading {
  display: inline-block;
  width: 20px;
  height: 20px;
  border: 3px solid rgba(255,255,255,.3);
  border-radius: 50%;
  border-top-color: var(--accent);
  animation: spin 1s ease-in-out infinite;
}

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

/* Welcome Message */
.welcome-message {
  margin: 2rem 0;
  text-align: center;
}

/* Scrollbar Styling */
.chat-container::-webkit-scrollbar {
  width: 6px;
}

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

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

.chat-container::-webkit-scrollbar-thumb:hover {
  background: var(--accent);
}

/* Error state */
.error-message {
  background: #ffebee;
  color: #c62828;
  padding: 0.75rem;
  border-radius: 8px;
  margin: 0.5rem;
  font-size: 0.875rem;
  text-align: center;
}

[data-theme="dark"] .error-message {
  background: #3a1f1f;
  color: #ff8a80;
}

/* Floating Add Button */
.floating-add-btn {
  position: fixed;
  bottom: 80px;
  right: 20px;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--accent);
  border: none;
  color: white;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow);
  transition: all var(--transition);
  z-index: 1000;
}

.floating-add-btn:hover {
  transform: scale(1.1);
  box-shadow: 0 4px 20px rgba(0,0,0,0.15);
}

@media (min-width: 768px) {
  .floating-add-btn {
    bottom: 100px;
    right: 50%;
    transform: translateX(240px);
  }
  
  .floating-add-btn:hover {
    transform: translateX(240px) scale(1.1);
  }
}

/* Modal Styles */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  visibility: hidden;
  transition: all 0.3s ease;
  z-index: 2000;
  padding: 1rem;
}

.modal-overlay.active {
  opacity: 1;
  visibility: visible;
}

.modal-content {
  background: var(--container-bg);
  border-radius: var(--radius);
  width: 100%;
  max-width: 500px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: var(--shadow);
  transform: scale(0.9) translateY(20px);
  transition: transform 0.3s ease;
}

.modal-overlay.active .modal-content {
  transform: scale(1) translateY(0);
}

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

.modal-header h2 {
  font-size: 1.25rem;
  font-weight: 600;
  color: var(--accent);
}

.modal-close {
  width: 32px;
  height: 32px;
  border: none;
  background: transparent;
  color: var(--text);
  cursor: pointer;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color var(--transition);
}

.modal-close:hover {
  background: var(--border);
}

.modal-body {
  padding: 1.5rem;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 1rem;
  padding: 1.5rem;
  border-top: 1px solid var(--border);
}

/* Journal Textarea */
.journal-textarea {
  width: 100%;
  min-height: 120px;
  padding: 1rem;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--bg);
  color: var(--text);
  font-family: inherit;
  font-size: 1rem;
  line-height: 1.5;
  resize: vertical;
  outline: none;
  transition: border-color var(--transition);
  margin-bottom: 1.5rem;
}

.journal-textarea:focus {
  border-color: var(--accent);
}

.journal-textarea::placeholder {
  color: var(--text);
  opacity: 0.5;
  font-style: italic;
}

/* Mood Section */
.mood-section {
  margin-bottom: 1.5rem;
}

.mood-label {
  display: block;
  font-weight: 600;
  margin-bottom: 1rem;
  color: var(--text);
  font-style: italic;
}

.mood-selector {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 0.75rem;
}

.mood-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
  padding: 1rem 0.75rem;
  border: 2px solid var(--border);
  border-radius: 12px;
  background: var(--bg);
  color: var(--text);
  cursor: pointer;
  transition: all var(--transition);
  font-family: inherit;
}

.mood-btn:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow);
}

.mood-btn.selected {
  border-color: var(--accent);
  background: var(--accent);
  color: white;
}

.mood-emoji {
  font-size: 1.5rem;
}

.mood-name {
  font-size: 0.875rem;
  font-weight: 500;
}

/* Tags Section */
.tags-section {
  margin-bottom: 1rem;
}

.tags-label {
  display: block;
  font-weight: 600;
  margin-bottom: 0.75rem;
  color: var(--text);
}

.tags-input-container {
  position: relative;
}

.tags-input {
  width: 100%;
  padding: 0.75rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font-family: inherit;
  outline: none;
  transition: border-color var(--transition);
}

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

.tags-display {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 0.75rem;
}

.tag-chip {
  background: var(--accent);
  color: white;
  padding: 0.25rem 0.75rem;
  border-radius: 16px;
  font-size: 0.875rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.tag-remove {
  background: none;
  border: none;
  color: white;
  cursor: pointer;
  padding: 0;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
}

.tag-remove:hover {
  background: rgba(255, 255, 255, 0.2);
}

/* Modal Buttons */
.btn-primary,
.btn-secondary {
  padding: 0.75rem 1.5rem;
  border-radius: 8px;
  font-family: inherit;
  font-weight: 500;
  cursor: pointer;
  transition: all var(--transition);
  border: none;
}

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

.btn-primary:hover {
  background: color-mix(in srgb, var(--accent) 90%, black);
  transform: translateY(-1px);
}

.btn-secondary {
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border);
}

.btn-secondary:hover {
  background: var(--border);
}

/* Memory Garden */
.memory-garden {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--container-bg);
  z-index: 1500;
  transform: translateX(100%);
  transition: transform 0.3s ease;
  display: flex;
  flex-direction: column;
}

.memory-garden.active {
  transform: translateX(0);
}

.memory-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem;
  padding-top: max(1rem, env(safe-area-inset-top));
  border-bottom: 1px solid var(--border);
  background: var(--container-bg);
}

.memory-header h2 {
  font-size: 1.25rem;
  font-weight: 600;
  color: var(--accent);
}

.memory-close {
  width: 40px;
  height: 40px;
  border: none;
  background: transparent;
  color: var(--text);
  cursor: pointer;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color var(--transition);
}

.memory-close:hover {
  background: var(--border);
}

.memory-filters {
  padding: 1rem;
  border-bottom: 1px solid var(--border);
  background: var(--container-bg);
}

.search-container {
  margin-bottom: 1rem;
}

.memory-search {
  width: 100%;
  padding: 0.75rem 1rem;
  border: 1px solid var(--border);
  border-radius: 22px;
  background: var(--bg);
  color: var(--text);
  font-family: inherit;
  outline: none;
  transition: border-color var(--transition);
}

.memory-search:focus {
  border-color: var(--accent);
}

.filter-buttons {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.filter-btn {
  padding: 0.5rem 1rem;
  border: 1px solid var(--border);
  border-radius: 20px;
  background: transparent;
  color: var(--text);
  cursor: pointer;
  font-family: inherit;
  font-size: 0.875rem;
  transition: all var(--transition);
}

.filter-btn:hover,
.filter-btn.active {
  background: var(--accent);
  color: white;
  border-color: var(--accent);
}

.memory-timeline {
  flex: 1;
  overflow-y: auto;
  padding: 1rem;
  -webkit-overflow-scrolling: touch;
}

/* Timeline Entry */
.timeline-entry {
  display: flex;
  gap: 1rem;
  margin-bottom: 1.5rem;
  padding: 1rem;
  border-radius: 12px;
  background: var(--bg);
  border-left: 4px solid var(--accent);
  transition: all var(--transition);
}

.timeline-entry:hover {
  transform: translateX(4px);
  box-shadow: var(--shadow);
}

.timeline-mood {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.25rem;
  min-width: 60px;
}

.timeline-mood-icon {
  font-size: 1.5rem;
  background: var(--container-bg);
  padding: 0.5rem;
  border-radius: 50%;
  border: 2px solid var(--border);
}

.timeline-date {
  font-size: 0.75rem;
  color: var(--text);
  opacity: 0.7;
  text-align: center;
}

.timeline-content {
  flex: 1;
}

.timeline-preview {
  font-size: 0.9rem;
  color: var(--text);
  opacity: 0.8;
  line-height: 1.4;
  margin-bottom: 0.5rem;
}

.timeline-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem;
}

.timeline-tag {
  background: var(--border);
  color: var(--text);
  padding: 0.125rem 0.5rem;
  border-radius: 12px;
  font-size: 0.75rem;
}

.timeline-expand {
  background: none;
  border: none;
  color: var(--accent);
  cursor: pointer;
  font-size: 0.875rem;
  margin-top: 0.5rem;
  text-decoration: underline;
}

.timeline-full-text {
  margin-top: 0.75rem;
  padding-top: 0.75rem;
  border-top: 1px solid var(--border);
  line-height: 1.5;
  display: none;
}

.timeline-full-text.expanded {
  display: block;
}

/* Enhanced Message Display */
.message.with-mood {
  position: relative;
}

.message-mood-indicator {
  position: absolute;
  top: -8px;
  right: -8px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--container-bg);
  border: 2px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.875rem;
}

.message-tags {
  margin-top: 0.5rem;
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem;
}

.message-tag {
  background: rgba(255, 255, 255, 0.1);
  color: inherit;
  padding: 0.125rem 0.5rem;
  border-radius: 10px;
  font-size: 0.75rem;
  opacity: 0.8;
}

@media (min-width: 768px) {
  .memory-garden {
    right: 0;
    left: auto;
    width: 400px;
    box-shadow: var(--shadow);
  }
  
  .modal-content {
    max-width: 600px;
  }
  
  .mood-selector {
    grid-template-columns: repeat(3, 1fr);
  }
}
