/* ─── Reset & Base ───────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --accent:           #3b82f6;
  --accent-hover:     #2563eb;
  --accent-subtle:    #eff6ff;
  --bg:               #f8fafc;
  --surface:          #ffffff;
  --border:           #e2e8f0;
  --border-subtle:    #f1f5f9;
  --text:             #0f172a;
  --text-muted:       #64748b;
  --text-subtle:      #94a3b8;
  --user-bg:          #3b82f6;
  --user-text:        #ffffff;
  --asst-bg:          #ffffff;
  --asst-text:        #0f172a;
  --green:            #10b981;
  --amber:            #f59e0b;
  --red:              #ef4444;
  --shadow-xs:        0 1px 2px rgba(0,0,0,.06);
  --shadow-sm:        0 1px 3px rgba(0,0,0,.1), 0 1px 2px rgba(0,0,0,.06);
  --shadow-md:        0 4px 6px -1px rgba(0,0,0,.07), 0 2px 4px -2px rgba(0,0,0,.05);
  --r-sm:             6px;
  --r-md:             12px;
  --r-lg:             18px;
  --r-xl:             24px;
  --font-sans:        system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --font-mono:        "SF Mono", "Fira Code", "Fira Mono", Consolas, "Liberation Mono", monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg:             #0f172a;
    --surface:        #1e293b;
    --border:         #334155;
    --border-subtle:  #1e293b;
    --text:           #f1f5f9;
    --text-muted:     #94a3b8;
    --text-subtle:    #64748b;
    --accent-subtle:  #172554;
    --asst-bg:        #1e293b;
    --asst-text:      #f1f5f9;
    --shadow-xs:      0 1px 2px rgba(0,0,0,.4);
    --shadow-sm:      0 1px 3px rgba(0,0,0,.4), 0 1px 2px rgba(0,0,0,.3);
    --shadow-md:      0 4px 6px -1px rgba(0,0,0,.5), 0 2px 4px -2px rgba(0,0,0,.4);
  }
}

html, body {
  height: 100%;
  font-family: var(--font-sans);
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* ─── App Shell ──────────────────────────────────────────────────────────── */
.app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 820px;
  margin: 0 auto;
}

/* ─── Header ─────────────────────────────────────────────────────────────── */
.header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0.7rem 1.25rem;
  flex-shrink: 0;
  box-shadow: var(--shadow-xs);
  position: sticky;
  top: 0;
  z-index: 20;
}
.header-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}
.brand {
  display: flex;
  align-items: center;
  gap: 0.7rem;
  min-width: 0;
}
.brand-logo {
  width: 34px;
  height: 34px;
  color: var(--accent);
  flex-shrink: 0;
}
.brand-text {
  display: flex;
  flex-direction: column;
  min-width: 0;
}
.brand-name {
  font-size: 0.95rem;
  font-weight: 700;
  letter-spacing: -0.015em;
  line-height: 1.2;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.brand-tagline {
  font-size: 0.675rem;
  color: var(--text-muted);
  line-height: 1.2;
  white-space: nowrap;
}

/* Model badge */
.model-badge {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.28rem 0.7rem;
  font-size: 0.74rem;
  font-weight: 500;
  white-space: nowrap;
  flex-shrink: 0;
  max-width: 260px;
  overflow: hidden;
}
.badge-label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.badge-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
  transition: background 0.3s;
}
.badge-dot--loading { background: var(--amber); animation: pulse 1.5s ease-in-out infinite; }
.badge-dot--ready   { background: var(--green); }
.badge-dot--error   { background: var(--red); }
.badge-tier {
  font-size: 0.62rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--accent);
  background: var(--accent-subtle);
  border-radius: 4px;
  padding: 1px 5px;
  flex-shrink: 0;
}
.badge-tier:empty { display: none; }

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

/* ─── Main ───────────────────────────────────────────────────────────────── */
.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
}

/* ─── Loading Overlay ────────────────────────────────────────────────────── */
.loading-overlay {
  position: absolute;
  inset: 0;
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
  padding: 1.5rem;
  transition: opacity 0.4s ease;
}
.loading-overlay.hidden {
  opacity: 0;
  pointer-events: none;
}
.loading-overlay[style*="display:none"],
.loading-overlay[style*="display: none"] {
  display: none !important;
}

.loading-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-xl);
  padding: 2rem 2.5rem 2.25rem;
  max-width: 400px;
  width: 100%;
  text-align: center;
  box-shadow: var(--shadow-md);
}
.loading-icon {
  width: 56px;
  height: 56px;
  background: var(--accent-subtle);
  border-radius: var(--r-lg);
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 1.25rem;
  color: var(--accent);
}
.loading-icon svg { width: 28px; height: 28px; }
.loading-title {
  font-size: 1.2rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  margin-bottom: 0.45rem;
}
.loading-subtitle {
  font-size: 0.85rem;
  color: var(--text-muted);
  line-height: 1.5;
  margin-bottom: 1.25rem;
  min-height: 2.5em;
}

.progress-track {
  background: var(--border);
  border-radius: 999px;
  height: 6px;
  overflow: hidden;
  margin-bottom: 0.65rem;
}
.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--accent), var(--accent-hover));
  border-radius: 999px;
  transition: width 0.35s ease;
}
.loading-detail {
  font-size: 0.72rem;
  color: var(--text-subtle);
  min-height: 1.1em;
  line-height: 1.4;
}

/* ─── Chat Section ───────────────────────────────────────────────────────── */
.chat-section {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 1.25rem 1.25rem 0.5rem;
  scroll-behavior: smooth;
}
.chat-messages {
  display: flex;
  flex-direction: column;
  gap: 1.1rem;
  min-height: 100%;
}

/* ─── Messages ───────────────────────────────────────────────────────────── */
.message {
  display: flex;
  flex-direction: column;
  max-width: 84%;
  animation: msgIn 0.18s ease;
}
.message--user      { align-self: flex-end;   align-items: flex-end; }
.message--assistant { align-self: flex-start; align-items: flex-start; }

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

.message-bubble {
  padding: 0.7rem 1rem;
  border-radius: var(--r-md);
  font-size: 0.875rem;
  line-height: 1.65;
  word-wrap: break-word;
  overflow-wrap: break-word;
  white-space: pre-wrap;
}
.message--user .message-bubble {
  background: var(--user-bg);
  color: var(--user-text);
  border-bottom-right-radius: var(--r-sm);
}
.message--assistant .message-bubble {
  background: var(--asst-bg);
  color: var(--asst-text);
  border: 1px solid var(--border);
  border-bottom-left-radius: var(--r-sm);
  box-shadow: var(--shadow-xs);
}

/* Streaming cursor */
.message-bubble--streaming::after {
  content: '▋';
  animation: blink 0.7s step-end infinite;
  color: var(--accent);
  margin-left: 1px;
}
@keyframes blink {
  0%, 100% { opacity: 1; }
  50%       { opacity: 0; }
}

/* Typing indicator */
.typing-dots {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 2px;
}
.typing-dots span {
  display: inline-block;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--text-muted);
  animation: dotBounce 1.4s ease-in-out infinite;
}
.typing-dots span:nth-child(2) { animation-delay: 0.16s; }
.typing-dots span:nth-child(3) { animation-delay: 0.32s; }
@keyframes dotBounce {
  0%, 60%, 100% { transform: translateY(0);    opacity: 0.4; }
  30%           { transform: translateY(-5px); opacity: 1; }
}

/* Citations */
.message-citations {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
  margin-top: 0.45rem;
}
.citation-tag {
  font-family: var(--font-mono);
  font-size: 0.68rem;
  background: var(--accent-subtle);
  color: var(--accent);
  border-radius: 4px;
  padding: 2px 7px;
  border: 1px solid rgba(59,130,246,.18);
  letter-spacing: -0.01em;
}

/* ─── Performance Bar ────────────────────────────────────────────────────── */
.perf-bar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.9rem;
  padding: 0.35rem 1rem;
  background: var(--surface);
  border-top: 1px solid var(--border-subtle);
  font-size: 0.73rem;
  color: var(--text-muted);
  flex-shrink: 0;
}
.perf-metric {
  display: flex;
  align-items: baseline;
  gap: 0.28rem;
}
.perf-label  { font-weight: 500; }
.perf-value  { font-family: var(--font-mono); font-weight: 700; color: var(--text); font-size: 0.78rem; }
.perf-unit   { font-size: 0.68rem; color: var(--text-subtle); }
.perf-sep    { color: var(--border); font-size: 1rem; line-height: 1; }

/* ─── Footer / Input ─────────────────────────────────────────────────────── */
.footer {
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 0.75rem 1.25rem 0.9rem;
  flex-shrink: 0;
}
.input-form {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 0.45rem;
}
.question-input {
  flex: 1;
  min-width: 0;
  background: var(--bg);
  border: 1.5px solid var(--border);
  border-radius: var(--r-md);
  padding: 0.62rem 1rem;
  font-size: 0.875rem;
  font-family: var(--font-sans);
  color: var(--text);
  outline: none;
  transition: border-color 0.15s;
}
.question-input::placeholder { color: var(--text-subtle); }
.question-input:focus:not(:disabled) { border-color: var(--accent); }
.question-input:disabled { opacity: 0.5; cursor: not-allowed; }

.send-btn {
  width: 42px;
  height: 42px;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: var(--r-md);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s, transform 0.1s, opacity 0.15s;
}
.send-btn svg { width: 18px; height: 18px; }
.send-btn:hover:not(:disabled)  { background: var(--accent-hover); }
.send-btn:active:not(:disabled) { transform: scale(0.93); }
.send-btn:disabled { opacity: 0.45; cursor: not-allowed; }

.footer-note {
  font-size: 0.7rem;
  color: var(--text-subtle);
  text-align: center;
  line-height: 1.45;
}

/* ─── Screen-Reader Only ─────────────────────────────────────────────────── */
.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip: rect(0,0,0,0);
  white-space: nowrap;
  border: 0;
}

/* ─── Responsive ─────────────────────────────────────────────────────────── */
@media (max-width: 560px) {
  .header { padding: 0.6rem 1rem; }
  .brand-tagline { display: none; }
  .loading-card { padding: 1.5rem 1.25rem 1.75rem; }
  .chat-section { padding: 1rem 1rem 0.5rem; }
  .footer { padding: 0.65rem 1rem 0.8rem; }
  .message { max-width: 92%; }
}

@media (prefers-reduced-motion: reduce) {
  .message { animation: none; }
  .badge-dot--loading { animation: none; }
  .message-bubble--streaming::after { animation: none; }
  .typing-dots span { animation: none; opacity: 0.6; }
}
