/* Imigra Paraguai: white-first palette with the Paraguayan flag's red and blue as accents. */
:root {
  --red: #d52b1e;
  --blue: #0038a8;
  --blue-soft: #e8eefb;
  --red-soft: #fdecea;

  --bg: #ffffff;
  --surface: #ffffff;
  --surface-2: #f4f6fa;
  --text: #1a2233;
  --muted: #5f6b80;
  --border: #e3e7ef;
  --accent: var(--blue);
  --accent-contrast: #ffffff;
  --user-bubble: var(--blue);
  --user-text: #ffffff;
  --link: var(--blue);
  --danger: #b3261e;
  --danger-bg: var(--red-soft);
  --code-bg: #f4f6fa;
  --shadow: 0 1px 2px rgba(16, 32, 72, .05), 0 6px 20px rgba(16, 32, 72, .07);
  --radius: 18px;
  --maxw: 760px;
  --font: "Nunito", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, monospace;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --blue: #7ea2ff;
    --red: #ff7a6e;
    --blue-soft: #1a2640;
    --red-soft: #3a1f1f;
    --bg: #0f1522;
    --surface: #161e2e;
    --surface-2: #1c2638;
    --text: #e9edf5;
    --muted: #9aa6bd;
    --border: #273248;
    --accent-contrast: #0f1522;
    --user-bubble: #22386a;
    --user-text: #eef2ff;
    --danger: #ffb4ab;
    --code-bg: #1c2638;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 6px 20px rgba(0, 0, 0, .3);
  }
}

:root[data-theme="dark"] {
  --blue: #7ea2ff;
  --red: #ff7a6e;
  --blue-soft: #1a2640;
  --red-soft: #3a1f1f;
  --bg: #0f1522;
  --surface: #161e2e;
  --surface-2: #1c2638;
  --text: #e9edf5;
  --muted: #9aa6bd;
  --border: #273248;
  --accent-contrast: #0f1522;
  --user-bubble: #22386a;
  --user-text: #eef2ff;
  --danger: #ffb4ab;
  --code-bg: #1c2638;
}

* { box-sizing: border-box; }

html, body { height: 100%; margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.65 var(--font);
  -webkit-font-smoothing: antialiased;
}

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

/* ---------- flag stripe ---------- */
.flag-stripe { display: flex; height: 4px; flex: none; }
.flag-stripe span { flex: 1; }
.flag-stripe span:nth-child(1) { background: var(--red); }
.flag-stripe span:nth-child(2) { background: #ffffff; }
.flag-stripe span:nth-child(3) { background: var(--blue); }

/* ---------- top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(8px);
  position: sticky;
  top: 0;
  z-index: 2;
}

.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand-mark { display: grid; place-items: center; flex: none; }
.brand-text { display: flex; flex-direction: column; min-width: 0; line-height: 1.2; }
.brand h1 {
  font-size: 18px;
  font-weight: 800;
  margin: 0;
  color: var(--blue);
  letter-spacing: -0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.brand-sub { font-size: 12.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.ghost-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 14px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  color: var(--blue);
  font: 700 13.5px var(--font);
  cursor: pointer;
  flex: none;
}
.ghost-btn:hover { background: var(--blue-soft); border-color: transparent; }

/* ---------- messages ---------- */
.scroll { flex: 1; overflow-y: auto; overscroll-behavior: contain; }

.messages {
  max-width: var(--maxw);
  margin: 0 auto;
  padding: 24px 16px 32px;
  display: flex;
  flex-direction: column;
  gap: 22px;
}

.welcome { text-align: center; padding: 8vh 4px 0; }
.welcome-badge {
  width: 64px;
  height: 64px;
  margin: 0 auto 14px;
  display: grid;
  place-items: center;
  font-size: 32px;
  border-radius: 50%;
  background: var(--blue-soft);
}
.welcome h2 { font-size: 28px; font-weight: 800; margin: 0 0 8px; letter-spacing: -0.015em; }
.welcome p { color: var(--muted); margin: 0 auto; max-width: 520px; white-space: pre-line; }

.suggestions {
  margin: 26px auto 0;
  max-width: 600px;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}
.suggestion {
  text-align: left;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
  color: var(--text);
  font: 600 14.5px/1.4 var(--font);
  cursor: pointer;
  box-shadow: 0 1px 2px rgba(16, 32, 72, .04);
  transition: border-color .15s, background .15s, transform .15s;
}
.suggestion:hover { border-color: var(--blue); background: var(--blue-soft); transform: translateY(-1px); }
.suggestion::before { content: "→ "; color: var(--red); font-weight: 800; }

.msg { display: flex; flex-direction: column; min-width: 0; }
.msg.user { align-items: flex-end; }

.msg.user .bubble {
  background: var(--user-bubble);
  color: var(--user-text);
  padding: 10px 16px;
  border-radius: var(--radius) var(--radius) 6px var(--radius);
  max-width: 85%;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.msg.assistant .bubble { max-width: 100%; overflow-wrap: anywhere; }

.bubble > :first-child { margin-top: 0; }
.bubble > :last-child { margin-bottom: 0; }
.bubble p { margin: 0 0 12px; }
.bubble ul, .bubble ol { margin: 0 0 12px; padding-left: 22px; }
.bubble li { margin: 5px 0; }
.bubble li::marker { color: var(--blue); font-weight: 700; }
.bubble h1, .bubble h2, .bubble h3, .bubble h4 { font-size: 16.5px; font-weight: 800; margin: 20px 0 8px; color: var(--blue); }
.bubble strong { font-weight: 800; }
.bubble a { color: var(--link); }
.bubble blockquote { margin: 0 0 12px; padding: 8px 12px; border-left: 3px solid var(--red); background: var(--red-soft); border-radius: 0 10px 10px 0; }
.bubble table { border-collapse: collapse; margin: 0 0 12px; display: block; overflow-x: auto; }
.bubble th, .bubble td { border: 1px solid var(--border); padding: 6px 10px; text-align: left; }
.bubble th { background: var(--surface-2); font-weight: 700; }

.bubble code { font: 13px/1.5 var(--mono); background: var(--code-bg); padding: 1px 5px; border-radius: 5px; }

.code-wrap { position: relative; margin: 0 0 12px; }
.bubble pre {
  margin: 0;
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
  overflow-x: auto;
}
.bubble pre code { background: none; padding: 0; }

.copy-btn {
  position: absolute;
  top: 6px;
  right: 6px;
  padding: 3px 8px;
  font: 700 11px var(--font);
  color: var(--muted);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
  opacity: 0;
  transition: opacity .15s;
}
.code-wrap:hover .copy-btn, .copy-btn:focus-visible { opacity: 1; }
@media (hover: none) { .copy-btn { opacity: 1; } }

.sources { margin-top: 12px; display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.sources-label { font-size: 12.5px; color: var(--muted); margin-right: 2px; }
.source-chip {
  font-size: 12.5px;
  color: var(--blue);
  background: var(--blue-soft);
  border-radius: 999px;
  padding: 2px 10px;
  max-width: 100%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.error-note {
  margin-top: 8px;
  padding: 10px 14px;
  border-radius: 12px;
  background: var(--danger-bg);
  color: var(--danger);
  font-size: 14.5px;
}

.typing { display: inline-flex; gap: 5px; padding: 8px 0; }
.typing span { width: 7px; height: 7px; border-radius: 50%; animation: blink 1.2s infinite ease-in-out; }
.typing span:nth-child(1) { background: var(--red); }
.typing span:nth-child(2) { background: var(--muted); animation-delay: .15s; }
.typing span:nth-child(3) { background: var(--blue); animation-delay: .3s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }

/* ---------- composer ---------- */
.composer-wrap { padding: 8px 16px calc(12px + env(safe-area-inset-bottom)); background: var(--bg); }

.composer {
  max-width: var(--maxw);
  margin: 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--surface);
  border: 1.5px solid var(--border);
  border-radius: 22px;
  padding: 8px 8px 8px 16px;
  box-shadow: var(--shadow);
}
.composer:focus-within { border-color: var(--blue); }

.composer textarea {
  flex: 1;
  border: 0;
  outline: 0;
  resize: none;
  background: transparent;
  color: var(--text);
  font: 16px/1.5 var(--font);
  padding: 6px 0;
  max-height: 200px;
}
.composer textarea::placeholder { color: var(--muted); }

.send-btn {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 50%;
  background: var(--blue);
  color: var(--accent-contrast);
  cursor: pointer;
  flex: none;
}
.send-btn:disabled { opacity: .35; cursor: default; }
.send-btn.busy { background: var(--red); }
.send-btn .icon-stop { display: none; }
.send-btn.busy .icon-send { display: none; }
.send-btn.busy .icon-stop { display: block; }

.disclaimer { text-align: center; color: var(--muted); font-size: 12px; margin: 8px auto 0; max-width: 560px; }

@media (max-width: 560px) {
  .ghost-btn span { display: none; }
  .ghost-btn { padding: 8px 10px; }
  .welcome { padding-top: 4vh; }
  .welcome h2 { font-size: 23px; }
  .suggestions { grid-template-columns: 1fr; }
  .brand-sub { display: none; }
}
