/* ============================================
   Carl — BioVillage Character System
   Deep indigo cosmos, pixel aesthetic
   ============================================ */

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

:root {
  --bg-deep: #0a0a1a;
  --bg-surface: rgba(12, 12, 32, 0.85);
  --bg-input: rgba(18, 18, 44, 0.9);
  --border-subtle: rgba(100, 120, 180, 0.15);
  --border-focus: rgba(140, 170, 255, 0.35);
  --text-primary: #d4d8f0;
  --text-muted: #7a7f9e;
  --text-faint: #4a4e6a;
  --accent: #8ba4e8;
  --accent-dim: rgba(139, 164, 232, 0.12);
  --carl-text: #c8cce8;
  --user-text: #a0b4e0;
  --user-bubble: rgba(70, 90, 140, 0.2);
  --font-body: 'General Sans', -apple-system, sans-serif;
  --transition-fast: 180ms cubic-bezier(0.16, 1, 0.3, 1);
  --radius-sm: 0.375rem;
  --radius-md: 0.625rem;
  --radius-lg: 1rem;
}

html {
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
  scroll-behavior: smooth;
}

body {
  min-height: 100dvh;
  font-family: var(--font-body);
  font-size: 15px;
  line-height: 1.6;
  color: var(--text-primary);
  background: var(--bg-deep);
  overflow: hidden;
}

/* ---- Loading Screen ---- */
#loading-screen {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-deep);
  transition: opacity 600ms ease, visibility 600ms ease;
}

#loading-screen.fade-out {
  opacity: 0;
  visibility: hidden;
}

.loading-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2rem;
}

.loading-text {
  font-family: var(--font-body);
  font-size: 1.125rem;
  font-weight: 500;
  color: var(--text-muted);
  letter-spacing: 0.08em;
}

.loading-dots::after {
  content: '';
  animation: dots 1.5s steps(4, end) infinite;
}

@keyframes dots {
  0%   { content: ''; }
  25%  { content: '.'; }
  50%  { content: '..'; }
  75%  { content: '...'; }
  100% { content: ''; }
}

.proceed-btn {
  font-family: var(--font-body);
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--text-primary);
  background: rgba(139, 164, 232, 0.1);
  border: 1px solid rgba(139, 164, 232, 0.25);
  border-radius: var(--radius-md);
  padding: 0.625rem 2rem;
  cursor: pointer;
  letter-spacing: 0.04em;
  transition: background 250ms ease, border-color 250ms ease, transform 150ms ease, opacity 500ms ease;
  opacity: 0;
}

.proceed-btn:not(.hidden) {
  opacity: 1;
}

.proceed-btn:hover {
  background: rgba(139, 164, 232, 0.2);
  border-color: rgba(139, 164, 232, 0.4);
}

.proceed-btn:active {
  transform: scale(0.97);
}

#app.hidden {
  opacity: 0;
  pointer-events: none;
}

#app {
  transition: opacity 600ms ease;
}

/* ---- Starfield Canvas ---- */
#starfield {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
  pointer-events: none;
}

/* ---- Main Layout ---- */
#app {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  max-width: 520px;
  margin: 0 auto;
  padding: 1rem 1.25rem 1rem;
  height: 100dvh;
}

/* ---- Billboard Hero ---- */
.billboard-container {
  display: flex;
  justify-content: center;
  margin-bottom: 0.75rem;
  width: 100%;
}

.carl-billboard {
  width: 100%;
  max-width: 480px;
  height: auto;
  border-radius: var(--radius-lg);
  border: 1px solid rgba(139, 92, 246, 0.2);
  box-shadow:
    0 0 40px rgba(139, 92, 246, 0.08),
    0 4px 24px rgba(0, 0, 0, 0.4);
  image-rendering: auto;
  animation: billboard-enter 800ms cubic-bezier(0.16, 1, 0.3, 1) both;
}

@keyframes billboard-enter {
  from {
    opacity: 0;
    transform: scale(0.96) translateY(-6px);
  }
  to {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
}

/* ---- Header ---- */
.carl-header {
  text-align: center;
  margin-bottom: 1.5rem;
}

.greeting {
  font-size: 1.375rem;
  font-weight: 600;
  color: var(--text-primary);
  letter-spacing: -0.01em;
  line-height: 1.3;
  margin-bottom: 0.25rem;
}

.subtext {
  font-size: 0.8125rem;
  color: var(--text-muted);
  font-weight: 400;
}

/* ---- Cosmic Destination Card ---- */
.destination-card {
  width: 100%;
  background: rgba(15, 15, 40, 0.7);
  border: 1px solid rgba(139, 92, 246, 0.25);
  border-radius: var(--radius-lg);
  overflow: hidden;
  margin-bottom: 1rem;
  transition: opacity 500ms ease, transform 500ms cubic-bezier(0.16, 1, 0.3, 1);
  flex-shrink: 0;
}

.destination-card.hidden {
  display: none;
}

.destination-card.entering {
  opacity: 0;
  transform: translateY(-10px) scale(0.98);
}

.dest-header {
  padding: 0.5rem 0.875rem;
  border-bottom: 1px solid rgba(139, 92, 246, 0.12);
}

.dest-label {
  font-size: 0.625rem;
  font-weight: 600;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: rgba(139, 92, 246, 0.6);
}

.dest-body {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem 0.875rem;
}

.dest-emoji {
  font-size: 2rem;
  line-height: 1;
  flex-shrink: 0;
}

.dest-info {
  display: flex;
  flex-direction: column;
  gap: 0.125rem;
}

.dest-name {
  font-size: 1.125rem;
  font-weight: 600;
  color: #e0d4f0;
  letter-spacing: -0.01em;
}

.dest-subtitle {
  font-size: 0.75rem;
  color: rgba(200, 180, 240, 0.5);
  font-style: italic;
}

.dest-stats {
  display: flex;
  flex-wrap: wrap;
  gap: 0.375rem;
  padding: 0 0.875rem 0.75rem;
}

.dest-stat {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 0.25rem 0.5rem;
  border-radius: var(--radius-sm);
  min-width: 3.25rem;
}

.dest-stat.good {
  background: rgba(74, 240, 192, 0.08);
  border: 1px solid rgba(74, 240, 192, 0.15);
}

.dest-stat.warn {
  background: rgba(255, 140, 66, 0.08);
  border: 1px solid rgba(255, 140, 66, 0.15);
}

.dest-stat-val {
  font-size: 0.8125rem;
  font-weight: 600;
  color: var(--text-primary);
  line-height: 1.2;
}

.dest-stat-label {
  font-size: 0.5625rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

/* Auto-triggered message (subtle) */
.message.auto-msg {
  opacity: 0.4;
  font-size: 0.8125rem;
}

/* ---- Chat Area ---- */
.chat-area {
  flex: 1;
  width: 100%;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 0.5rem 0;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  scrollbar-width: thin;
  scrollbar-color: rgba(100, 120, 180, 0.15) transparent;
  mask-image: linear-gradient(to bottom, transparent 0%, black 3%, black 97%, transparent 100%);
  -webkit-mask-image: linear-gradient(to bottom, transparent 0%, black 3%, black 97%, transparent 100%);
}

.chat-area::-webkit-scrollbar {
  width: 4px;
}
.chat-area::-webkit-scrollbar-track {
  background: transparent;
}
.chat-area::-webkit-scrollbar-thumb {
  background: rgba(100, 120, 180, 0.2);
  border-radius: 2px;
}

/* ---- Message Bubbles ---- */
.message {
  max-width: 92%;
  animation: msg-enter 400ms cubic-bezier(0.16, 1, 0.3, 1) both;
}

.message-user {
  align-self: flex-end;
  background: var(--user-bubble);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg) var(--radius-lg) var(--radius-sm) var(--radius-lg);
  padding: 0.625rem 0.875rem;
  color: var(--user-text);
  font-size: 0.875rem;
}

.message-carl {
  align-self: flex-start;
  padding: 0.375rem 0;
}

.message-carl .carl-name {
  font-size: 0.6875rem;
  font-weight: 600;
  color: var(--accent);
  letter-spacing: 0.05em;
  text-transform: uppercase;
  margin-bottom: 0.25rem;
}

.message-carl .carl-text {
  font-size: 0.9375rem;
  color: var(--carl-text);
  line-height: 1.65;
  white-space: pre-wrap;
}

/* Cursor blink during streaming */
.carl-text.streaming::after {
  content: "▍";
  color: var(--accent);
  animation: blink 1s step-end infinite;
  font-weight: 300;
}

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

@keyframes msg-enter {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* ---- Input Area ---- */
.input-area {
  width: 100%;
  display: flex;
  gap: 0.5rem;
  padding: 0.75rem 0;
  flex-shrink: 0;
}

#chat-input {
  flex: 1;
  padding: 0.625rem 0.875rem;
  font-size: 0.875rem;
  font-family: var(--font-body);
  color: var(--text-primary);
  background: var(--bg-input);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  outline: none;
  transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
}

#chat-input::placeholder {
  color: var(--text-faint);
}

#chat-input:focus {
  border-color: var(--border-focus);
  box-shadow: 0 0 0 3px rgba(139, 164, 232, 0.08);
}

#send-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  background: var(--accent-dim);
  color: var(--accent);
  cursor: pointer;
  flex-shrink: 0;
  transition: background var(--transition-fast), border-color var(--transition-fast), transform var(--transition-fast);
}

#send-btn:hover {
  background: rgba(139, 164, 232, 0.2);
  border-color: var(--border-focus);
}

#send-btn:active {
  transform: scale(0.95);
}

#send-btn:disabled {
  opacity: 0.3;
  cursor: not-allowed;
  pointer-events: none;
}

/* ---- Back Link ---- */
.back-link {
  padding: 0.5rem 0 0.25rem;
  flex-shrink: 0;
}

.back-link a {
  display: inline-flex;
  align-items: center;
  gap: 0.375rem;
  font-size: 0.75rem;
  color: var(--text-faint);
  text-decoration: none;
  transition: color var(--transition-fast);
}

.back-link a:hover {
  color: var(--text-muted);
}

/* ---- Attribution ---- */
.attribution {
  text-align: center;
  padding: 0 0 0.5rem;
  flex-shrink: 0;
}

.attribution a {
  font-size: 0.625rem;
  color: var(--text-faint);
  text-decoration: none;
  opacity: 0.4;
  transition: opacity var(--transition-fast);
}

.attribution a:hover {
  opacity: 0.7;
}

/* ---- Focus Visible ---- */
:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: var(--radius-sm);
}

/* ---- Selection ---- */
::selection {
  background: rgba(139, 164, 232, 0.25);
  color: var(--text-primary);
}

/* ---- Reduced Motion ---- */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* ---- Mobile ---- */
@media (max-width: 480px) {
  #app {
    padding: 1.25rem 1rem 0.75rem;
  }

  .greeting {
    font-size: 1.25rem;
  }

  .carl-header {
    margin-bottom: 1rem;
  }

  .carl-billboard {
    max-width: 100%;
    border-radius: var(--radius-md);
  }
}
