/* ============================================================================
   Engine Maintenance Chat — Minimal & Domain-Aware
   ============================================================================ */

:root {
  --bg: #fafafa;
  --bg-elevated: #ffffff;
  --fg: #1a1a2e;
  --fg-muted: #6b7280;
  --border: #e5e7eb;
  --accent: #0f766e;
  --accent-hover: #115e59;
  --accent-light: #ecfdf5;
  --danger: #b91c1c;
  --danger-light: #fef2f2;
  --warning: #b45309;
  --warning-light: #fffbeb;
  --nominal: #065f46;
  --nominal-light: #ecfdf5;

  --font-ui: 'IBM Plex Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --font-mono: 'IBM Plex Mono', 'JetBrains Mono', monospace;

  --space-2: 0.5rem;
  --space-3: 0.75rem;
  --space-4: 1rem;
  --space-5: 1.25rem;
  --space-6: 1.5rem;

  --radius: 8px;
  --radius-sm: 4px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f172a;
    --bg-elevated: #1e293b;
    --fg: #f1f5f9;
    --fg-muted: #94a3b8;
    --border: #334155;
    --accent: #14b8a6;
    --accent-hover: #0d9488;
    --accent-light: #0f172a;
    --danger: #f87171;
    --danger-light: #450a0a;
    --warning: #fbbf24;
    --warning-light: #451a03;
    --nominal: #34d399;
    --nominal-light: #0f172a;
  }
}

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

html { font-size: 15px; }

body {
  font-family: var(--font-ui);
  font-size: 1rem;
  line-height: 1.6;
  color: var(--fg);
  background: var(--bg);
  height: 100vh;
  display: flex;
  flex-direction: column;
}

/* ============================================================================
   App Layout
   ============================================================================ */

.app {
  display: flex;
  flex-direction: column;
  height: 100%;
  width: 100%;
}

/* ============================================================================
   Header — Engine telemetry vibe
   ============================================================================ */

.header {
  display: flex;
  align-items: center;
  gap: var(--space-4);
  padding: var(--space-4) var(--space-5);
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.header-title {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  min-width: 0;
}

.header-title h1 {
  font-size: 1.125rem;
  font-weight: 600;
  letter-spacing: -0.02em;
  color: var(--fg);
}

.header-title .status {
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--nominal);
  background: var(--nominal-light);
  padding: 2px 8px;
  border-radius: var(--radius-sm);
}

.header-actions { margin-left: auto; display: flex; gap: var(--space-2); align-items: center; }

.ai-profile {
  position: relative;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--fg-muted);
  flex-shrink: 0;
}
.ai-profile svg { width: 18px; height: 18px; }

/* Blinking indicator dot at bottom-right of AI profile */
.ai-indicator {
  position: absolute;
  bottom: 0;
  right: 0;
  width: 10px;
  height: 10px;
  background: var(--accent);
  border: 2px solid var(--bg);
  border-radius: 50%;
  opacity: 0;
  transform: scale(0);
  transition: opacity 150ms, transform 150ms;
}
.ai-profile.streaming .ai-indicator {
  opacity: 1;
  transform: scale(1);
  animation: blink 1.2s ease-in-out infinite;
}

@keyframes blink {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.35; transform: scale(0.8); }
}

.btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  border-radius: var(--radius);
  color: var(--fg-muted);
  cursor: pointer;
}
.db-select {
  height: 36px;
  padding: 0 10px;
  font-family: var(--font-ui);
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--fg);
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  min-width: 120px;
}
.db-select:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-light); outline: none; }
.btn:hover { background: var(--bg); border-color: var(--fg-muted); color: var(--fg); }
.btn svg { width: 18px; height: 18px; }

/* ============================================================================
   Messages
   ============================================================================ */

.messages {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-5);
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
  min-height: 0;
}

.message {
  display: flex;
  gap: var(--space-3);
  animation: none;
  line-height: 1.6;
}

.message--user { flex-direction: row-reverse; }

.message-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.875rem;
  position: relative;
}
.message--user .message-avatar { background: var(--accent-light); color: var(--accent); }
.message--assistant .message-avatar { background: var(--bg-elevated); border: 1px solid var(--border); color: var(--fg-muted); }

/* Streaming indicator on bot avatar */
.message--assistant .message-avatar.streaming::after {
  content: '';
  position: absolute;
  bottom: -2px;
  right: -2px;
  width: 10px;
  height: 10px;
  background: var(--accent);
  border: 2px solid var(--bg);
  border-radius: 50%;
  animation: blink 1s ease-in-out infinite;
}

@keyframes blink {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.4; transform: scale(0.85); }
}

.message-content {
  max-width: 80%;
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius);
  font-size: 0.9375rem;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}
.message--user .message-content { background: var(--accent); color: #fff; padding-left: var(--space-4); padding-right: var(--space-3); }
.message--assistant .message-content { background: var(--bg-elevated); border: 1px solid var(--border); color: var(--fg); padding-left: var(--space-5); }

/* Markdown */
.message-content p { margin: var(--space-2) 0; }
.message-content p:first-child { margin-top: 0; }
.message-content p:last-child { margin-bottom: 0; }
.message-content strong { font-weight: 600; }
.message-content em { font-style: italic; }
.message-content code {
  font-family: var(--font-mono);
  font-size: 0.85em;
  background: rgba(0,0,0,0.06);
  padding: 0.15em 0.45em;
  border-radius: var(--radius-sm);
}
.message--user .message-content code { background: rgba(255,255,255,0.2); color: inherit; }
.message-content pre {
  font-family: var(--font-mono);
  font-size: 0.8125rem;
  line-height: 1.6;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: var(--space-3);
  overflow-x: auto;
  margin: var(--space-3) 0;
  max-width: 100%;
}
.message-content pre code { background: transparent; padding: 0; font-size: inherit; color: var(--fg); }
.message-content table { border-collapse: collapse; width: 100%; font-size: 0.8125rem; margin: var(--space-3) 0; table-layout: fixed; }
.message-content th, .message-content td { border: 1px solid var(--border); padding: var(--space-2) var(--space-3); text-align: left; word-wrap: break-word; overflow-wrap: anywhere; }
.message-content th { background: var(--bg); font-weight: 600; color: var(--fg-muted); font-size: 0.6875rem; text-transform: uppercase; letter-spacing: 0.03em; }
.message-content tr:nth-child(even) td { background: var(--bg); }

/* Engine status badges in messages */
.engine-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 0.75rem;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: var(--radius-sm);
  margin-right: var(--space-2);
}
.engine-badge.nominal { background: var(--nominal-light); color: var(--nominal); }
.engine-badge.caution { background: var(--warning-light); color: var(--warning); }
.engine-badge.critical { background: var(--danger-light); color: var(--danger); }

/* Fleet status in header */
.status { display: inline-flex; align-items: center; gap: 4px; font-size: 0.7rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.08em; padding: 2px 8px; border-radius: var(--radius-sm); }
.status.nominal { background: var(--nominal-light); color: var(--nominal); }
.status.caution { background: var(--warning-light); color: var(--warning); }
.status.critical { background: var(--danger-light); color: var(--danger); }

/* ============================================================================
   Composer
   ============================================================================ */

.composer {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-4) var(--space-5);
  background: var(--bg-elevated);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

#message-input {
  flex: 1;
  min-height: 48px;
  max-height: 160px;
  padding: var(--space-3) var(--space-4);
  font-family: var(--font-ui);
  font-size: 1rem;
  line-height: 1.5;
  color: var(--fg);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  resize: none;
  transition: border-color 100ms, box-shadow 100ms;
}
#message-input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-light); outline: none; }
#message-input::placeholder { color: var(--fg-muted); }

.send-btn {
  width: 48px;
  height: 48px;
  border: none;
  background: var(--accent);
  border-radius: var(--radius);
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 100ms, transform 100ms;
}
.send-btn:hover:not(:disabled) { background: var(--accent-hover); }
.send-btn:active:not(:disabled) { transform: scale(0.97); }
.send-btn:disabled { opacity: 0.5; cursor: not-allowed; }
.send-btn svg { width: 20px; height: 20px; }

/* Streaming state - red stop button */
.send-btn.streaming {
  background: var(--danger);
}
.send-btn.streaming:hover:not(:disabled) {
  background: #dc2626;
}
.send-btn.streaming:active:not(:disabled) {
  transform: scale(0.97);
}

/* ============================================================================
   Responsive
   ============================================================================ */

@media (max-width: 640px) {
  .header { padding: var(--space-3); }
  .header-title h1 { font-size: 1rem; }
  .messages { padding: var(--space-4) var(--space-3); gap: var(--space-3); }
  .message-content { max-width: 85%; padding: var(--space-2) var(--space-3); }
  .composer { padding: var(--space-3); }
}

/* ============================================================================
   Reduced Motion
   ============================================================================ */

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}