/* Design tokens: light mode by default, dark mode follows the system setting. */
:root {
  color-scheme: light dark;

  --bg: #f6f8f7;
  --surface: #ffffff;
  --surface-muted: #f0f3f2;
  --border: #e1e7e4;
  --text: #17201c;
  --text-muted: #5b6a63;
  --accent: #059669;
  --accent-strong: #047857;
  --accent-soft: #d1fae5;
  --danger: #b42318;
  --danger-soft: #fef3f2;
  --shadow: 0 1px 2px rgb(16 24 20 / 0.04), 0 8px 24px rgb(16 24 20 / 0.06);

  --radius: 14px;
  --font: "Inter", system-ui, -apple-system, "Segoe UI", sans-serif;
  --mono: "JetBrains Mono", ui-monospace, "SF Mono", Menlo, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1211;
    --surface: #151c1a;
    --surface-muted: #1c2522;
    --border: #26322e;
    --text: #e6ece9;
    --text-muted: #94a39c;
    --accent: #34d399;
    --accent-strong: #6ee7b7;
    --accent-soft: rgb(52 211 153 / 0.14);
    --danger: #fda29b;
    --danger-soft: rgb(240 68 56 / 0.12);
    --shadow: 0 1px 2px rgb(0 0 0 / 0.3), 0 8px 24px rgb(0 0 0 / 0.25);
  }
}

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

body {
  margin: 0;
  background:
    radial-gradient(900px 400px at 50% -120px, var(--accent-soft), transparent 70%),
    var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

a {
  color: var(--accent-strong);
}

/* Elements with display: grid would ignore the hidden attribute otherwise. */
[hidden] {
  display: none !important;
}

.page {
  max-width: 760px;
  margin: 0 auto;
  padding: 56px 16px 40px;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

/* --- Language switcher (ES / EN) ---------------------------------------- */

.language {
  display: flex;
  justify-content: flex-end;
  gap: 2px;
  width: fit-content;
  margin: -32px 0 16px auto;
  padding: 3px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
}

.language-option {
  padding: 3px 10px;
  border: 0;
  border-radius: 999px;
  background: transparent;
  color: var(--text-muted);
  font: inherit;
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
}

.language-option[aria-pressed="true"] {
  background: var(--accent-soft);
  color: var(--accent-strong);
}

/* --- Header --------------------------------------------------------------- */

.hero {
  text-align: center;
  margin-bottom: 28px;
}

.badge {
  display: inline-block;
  padding: 4px 12px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  color: var(--text-muted);
  font-size: 0.8rem;
  font-weight: 500;
}

h1 {
  margin: 16px 0 8px;
  font-size: clamp(1.7rem, 4.5vw, 2.4rem);
  line-height: 1.15;
  letter-spacing: -0.02em;
  text-wrap: balance;
}

.lead {
  max-width: 560px;
  margin: 0 auto;
  color: var(--text-muted);
  text-wrap: pretty;
}

/* --- Cards and the question form ------------------------------------------ */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.ask {
  padding: 14px;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.ask:focus-within {
  border-color: var(--accent);
  box-shadow: var(--shadow), 0 0 0 4px var(--accent-soft);
}

textarea {
  display: block;
  width: 100%;
  min-height: 84px;
  padding: 6px 4px;
  border: 0;
  outline: 0;
  resize: vertical;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 1.05rem;
}

textarea::placeholder {
  color: var(--text-muted);
  opacity: 0.8;
}

.ask-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 8px;
}

.ask-options {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text-muted);
  font-size: 0.85rem;
}

select {
  padding: 4px 8px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-muted);
  color: var(--text);
  font: inherit;
}

.counter {
  margin-left: 6px;
  font-variant-numeric: tabular-nums;
}

.counter.near-limit {
  color: var(--danger);
}

.hint {
  margin: 8px 4px 0;
  color: var(--text-muted);
  font-size: 0.78rem;
}

.button {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 120px;
  padding: 10px 18px;
  border: 0;
  border-radius: 10px;
  background: var(--accent);
  color: #fff;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, transform 0.05s;
}

@media (prefers-color-scheme: dark) {
  .button {
    color: #04261b;
  }
}

.button:hover {
  background: var(--accent-strong);
}

.button:active {
  transform: translateY(1px);
}

.button:disabled {
  cursor: progress;
  opacity: 0.75;
}

.spinner {
  display: none;
  position: absolute;
  width: 18px;
  height: 18px;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

.button.is-loading .button-label {
  visibility: hidden;
}

.button.is-loading .spinner {
  display: block;
}

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

/* --- Example questions ---------------------------------------------------- */

.examples {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  margin: 16px 0 8px;
}

.chip {
  padding: 6px 12px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  color: var(--text-muted);
  font: inherit;
  font-size: 0.85rem;
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s;
}

.chip:hover {
  border-color: var(--accent);
  color: var(--text);
}

/* --- Result --------------------------------------------------------------- */

.result {
  display: grid;
  /* minmax(0, 1fr): a long code line scrolls inside its block instead of
     stretching the whole column past the screen on phones. */
  grid-template-columns: minmax(0, 1fr);
  gap: 16px;
  margin-top: 28px;
}

.asked {
  margin: 0;
  padding-left: 12px;
  border-left: 3px solid var(--accent);
  color: var(--text-muted);
  font-weight: 500;
  overflow-wrap: anywhere;
}

.section-title {
  margin: 0 0 10px;
  color: var(--text-muted);
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.answer-card,
.sources-card,
.error,
.skeleton {
  padding: 20px 22px;
}

.error {
  border-color: transparent;
  background: var(--danger-soft);
  color: var(--danger);
  font-weight: 500;
}

/* Loading placeholder */
.skeleton {
  display: grid;
  gap: 12px;
}

.skeleton .line {
  height: 12px;
  border-radius: 6px;
  background: linear-gradient(90deg, var(--surface-muted), var(--border), var(--surface-muted));
  background-size: 200% 100%;
  animation: shimmer 1.2s ease-in-out infinite;
}

.skeleton .line.short {
  width: 60%;
}

@keyframes shimmer {
  from {
    background-position: 100% 0;
  }
  to {
    background-position: -100% 0;
  }
}

/* --- Rendered Markdown ---------------------------------------------------- */

.markdown {
  overflow-wrap: anywhere;
}

.markdown > :first-child {
  margin-top: 0;
}

.markdown > :last-child {
  margin-bottom: 0;
}

.markdown p,
.markdown ul,
.markdown ol {
  margin: 0 0 12px;
}

.markdown ul,
.markdown ol {
  padding-left: 22px;
}

.markdown h1,
.markdown h2,
.markdown h3,
.markdown h4 {
  margin: 20px 0 8px;
  font-size: 1.05rem;
  line-height: 1.3;
}

.markdown blockquote {
  margin: 0 0 12px;
  padding: 4px 14px;
  border-left: 3px solid var(--border);
  color: var(--text-muted);
}

.markdown code {
  padding: 2px 6px;
  border-radius: 6px;
  background: var(--surface-muted);
  font-family: var(--mono);
  font-size: 0.87em;
}

.markdown pre {
  margin: 0 0 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow-x: auto;
}

.markdown pre code,
.markdown pre code.hljs {
  display: block;
  padding: 14px 16px;
  border-radius: 0;
  background: var(--surface-muted);
  font-size: 0.85rem;
  line-height: 1.55;
}

.markdown table {
  display: block;
  max-width: 100%;
  margin: 0 0 12px;
  overflow-x: auto;
  border-collapse: collapse;
}

.markdown th,
.markdown td {
  padding: 6px 10px;
  border: 1px solid var(--border);
}

/* Citation bubbles: [1] → ① */
.cite {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 1.35em;
  height: 1.35em;
  margin: 0 1px;
  padding: 0 4px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent-strong);
  font-size: 0.72em;
  font-weight: 700;
  text-decoration: none;
  vertical-align: super;
  line-height: 1;
}

.cite:hover {
  background: var(--accent);
  color: var(--surface);
}

/* --- Sources -------------------------------------------------------------- */

.sources {
  display: grid;
  gap: 4px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.source {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 12px;
  padding: 10px;
  border-radius: 10px;
  transition: background 0.15s;
}

.source:hover {
  background: var(--surface-muted);
}

.source.flash {
  animation: flash 1.4s ease-out;
}

@keyframes flash {
  from {
    background: var(--accent-soft);
  }
  to {
    background: transparent;
  }
}

.source-number {
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  border-radius: 8px;
  background: var(--accent-soft);
  color: var(--accent-strong);
  font-size: 0.85rem;
  font-weight: 700;
}

.source-body {
  display: grid;
  min-width: 0;
}

.source-title {
  overflow: hidden;
  font-weight: 600;
  text-decoration: none;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.source-title:hover {
  text-decoration: underline;
}

.source-path {
  overflow: hidden;
  color: var(--text-muted);
  font-family: var(--mono);
  font-size: 0.75rem;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Similarity score with a small bar behind the number. */
.score {
  position: relative;
  min-width: 56px;
  padding: 2px 8px;
  border-radius: 6px;
  background: linear-gradient(90deg, var(--accent-soft) var(--score), var(--surface-muted) var(--score));
  color: var(--text-muted);
  font-family: var(--mono);
  font-size: 0.75rem;
  text-align: center;
}

/* --- Footer --------------------------------------------------------------- */

.footer {
  margin-top: 48px;
  color: var(--text-muted);
  font-size: 0.8rem;
  text-align: center;
}

@media (max-width: 520px) {
  .page {
    padding-top: 32px;
  }

  .language {
    margin-top: -16px;
  }

  .ask-footer {
    flex-wrap: wrap;
  }

  .button {
    width: 100%;
  }

  .hint {
    display: none;
  }
}

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