:root {
  --bg: #faf9f7;
  --surface: #ffffff;
  --surface-2: #f1efeb;
  --text: #1d1f24;
  --muted: #5d6270;
  --faint: #8a8f9c;
  --border: #e2dfd9;
  --border-strong: #cfcbc3;
  --accent: #1f5fbf;
  --accent-text: #ffffff;
  --accent-soft: #e7eefb;
  --user-bg: #eeebe5;
  --err-bg: #fbeeee;
  --err-text: #9a2424;
  --err-border: #f0caca;
  --code-bg: #efece6;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.04), 0 4px 16px rgb(0 0 0 / 0.06);
  --col: 760px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17181b;
    --surface: #212226;
    --surface-2: #2a2b30;
    --text: #ececef;
    --muted: #a9adb8;
    --faint: #80848f;
    --border: #33353b;
    --border-strong: #464951;
    --accent: #86b4ff;
    --accent-text: #0e1420;
    --accent-soft: #1f2b40;
    --user-bg: #2c2d32;
    --err-bg: #3a1f21;
    --err-text: #ffb7b7;
    --err-border: #5a2c2f;
    --code-bg: #2c2d33;
    --shadow: 0 1px 2px rgb(0 0 0 / 0.3), 0 4px 16px rgb(0 0 0 / 0.35);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.6 var(--font);
  -webkit-font-smoothing: antialiased;
}
button { font: inherit; color: inherit; }
[hidden] { display: none !important; }
.visually-hidden {
  position: absolute !important; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

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

/* ---------- Barra in alto ---------- */
.topbar {
  flex: none; display: flex; align-items: center; justify-content: space-between; gap: 12px;
  height: 56px; padding: 0 16px; border-bottom: 1px solid var(--border); background: var(--bg);
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.logo {
  flex: none; display: grid; place-items: center; width: 28px; height: 28px; border-radius: 8px;
  background: var(--accent); color: var(--accent-text); font-weight: 700; font-size: 15px;
}
.brand-name { font-weight: 650; font-size: 1.05rem; letter-spacing: -0.01em; }
.brand-sub { color: var(--muted); font-size: 0.85rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.btn-new {
  flex: none; display: inline-flex; align-items: center; gap: 6px; min-height: 36px; padding: 6px 12px;
  border: 1px solid var(--border-strong); border-radius: 10px; background: var(--surface); cursor: pointer;
  font-size: 0.9rem;
}
.btn-new:hover { background: var(--surface-2); }

/* ---------- Area scorrevole ---------- */
.scroller { flex: 1; overflow-y: auto; overflow-x: hidden; overscroll-behavior: contain; }
.conversation { max-width: var(--col); margin: 0 auto; padding: 24px 16px 32px; }
.conversation:empty { padding: 0; }

/* ---------- Benvenuto ---------- */
.welcome {
  max-width: var(--col); margin: 0 auto; padding: 12vh 16px 32px;
  display: flex; flex-direction: column; align-items: center; text-align: center;
}
.welcome h1 { margin: 0 0 8px; font-size: clamp(1.6rem, 4vw, 2.1rem); font-weight: 600; letter-spacing: -0.02em; }
.welcome-sub { margin: 0 0 28px; color: var(--muted); max-width: 34rem; }
.cards {
  list-style: none; margin: 0; padding: 0; width: 100%;
  display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px;
}
.card {
  width: 100%; height: 100%; display: flex; flex-direction: column; gap: 6px; text-align: left;
  padding: 14px 16px; border: 1px solid var(--border); border-radius: 14px; background: var(--surface);
  cursor: pointer; transition: border-color 0.15s, background 0.15s, transform 0.15s;
}
.card:hover { border-color: var(--border-strong); background: var(--surface-2); }
.card:active { transform: translateY(1px); }
.card-tag { font-size: 0.75rem; font-weight: 600; color: var(--accent); text-transform: uppercase; letter-spacing: 0.04em; }
.card-q { font-size: 0.93rem; line-height: 1.45; }

/* ---------- Messaggi ---------- */
.turn { display: flex; flex-direction: column; gap: 14px; margin-bottom: 28px; }
.msg-user { display: flex; justify-content: flex-end; }
.bubble {
  max-width: 85%; padding: 10px 16px; border-radius: 18px; background: var(--user-bg);
  white-space: pre-wrap; overflow-wrap: anywhere;
}
.msg-bot { min-width: 0; }
.bot-head { display: flex; align-items: center; gap: 8px; margin-bottom: 4px; }
.avatar {
  display: grid; place-items: center; width: 24px; height: 24px; border-radius: 7px;
  background: var(--accent); color: var(--accent-text); font-weight: 700; font-size: 12px;
}
.bot-name { font-size: 0.85rem; font-weight: 600; color: var(--muted); }

/* ---------- Risposta (markdown) ---------- */
.md { overflow-wrap: anywhere; }
.md p { margin: 0 0 0.75em; }
.md ul, .md ol { margin: 0 0 0.75em; padding-left: 1.4em; }
.md li { margin: 0.2em 0; }
.md li::marker { color: var(--muted); }
.md .md-h { margin: 1em 0 0.4em; font-size: 1.05rem; line-height: 1.35; }
.md h4.md-h { font-size: 1rem; }
.md hr { border: 0; border-top: 1px solid var(--border); margin: 1em 0; }
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md code {
  font-family: var(--mono); font-size: 0.88em; padding: 0.1em 0.35em; border-radius: 5px; background: var(--code-bg);
}
.md strong { font-weight: 650; }

.cite {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 18px; height: 18px; margin: 0 1px 0 3px; padding: 0 5px; position: relative; top: -0.45em;
  font-size: 0.68rem; font-weight: 600; line-height: 1; cursor: pointer;
  color: var(--accent); background: var(--accent-soft); border: 0; border-radius: 9px;
  vertical-align: baseline;
}
.cite::after { content: ""; position: absolute; inset: -6px -2px; } /* area di tocco più ampia */
.cite:hover { background: var(--accent); color: var(--accent-text); }

/* ---------- Indicatore di scrittura ---------- */
.typing { display: inline-flex; gap: 5px; padding: 8px 0; }
.typing .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--faint); animation: blink 1.2s infinite ease-in-out; }
.typing .dot:nth-child(2) { animation-delay: 0.15s; }
.typing .dot:nth-child(3) { animation-delay: 0.3s; }
.typing { align-items: center; flex-wrap: wrap; }
.typing .step { margin-left: 6px; color: var(--muted); font-size: 0.875rem; animation: stepin 0.3s ease-out; }
.typing .step:empty { display: none; }
@keyframes stepin { from { opacity: 0; transform: translateY(3px); } to { opacity: 1; transform: none; } }
@keyframes blink { 0%, 80%, 100% { opacity: 0.25; transform: translateY(0); } 40% { opacity: 1; transform: translateY(-2px); } }
@media (prefers-reduced-motion: reduce) { .typing .dot { animation: none; opacity: 0.6; } .typing .step { animation: none; } .card { transition: none; } }

/* ---------- Fonti ---------- */
.sources { margin-top: 14px; }
.sources-sum {
  display: inline-flex; align-items: center; gap: 6px; cursor: pointer; list-style: none;
  font-size: 0.82rem; font-weight: 600; color: var(--muted); padding: 4px 10px 4px 8px;
  border: 1px solid var(--border); border-radius: 999px; background: var(--surface);
}
.sources-sum::-webkit-details-marker { display: none; }
.sources-sum::before { content: "▸"; font-size: 0.75rem; transition: transform 0.15s; }
.sources[open] > .sources-sum::before { transform: rotate(90deg); }
.sources-sum:hover { color: var(--text); border-color: var(--border-strong); }
.sources[open] > .sources-sum { margin-bottom: 8px; }
details.src { border: 1px solid var(--border); border-radius: 10px; margin-bottom: 6px; background: var(--surface); }
details.src > summary {
  display: flex; align-items: flex-start; gap: 8px; cursor: pointer; list-style: none;
  padding: 8px 12px; font-size: 0.85rem; color: var(--text);
}
details.src > summary::-webkit-details-marker { display: none; }
.src-n {
  flex: none; display: inline-grid; place-items: center; min-width: 20px; height: 20px; padding: 0 5px;
  margin-top: 1px; border-radius: 10px; font-size: 0.7rem; font-weight: 600;
  background: var(--accent-soft); color: var(--accent);
}
.src-label { min-width: 0; overflow-wrap: anywhere; }
details.src.cited { border-color: color-mix(in srgb, var(--accent) 45%, var(--border)); }
.src-body {
  padding: 0 12px 12px 40px; font-size: 0.84rem; line-height: 1.55; color: var(--muted);
  white-space: pre-wrap; overflow-wrap: anywhere; max-height: 18rem; overflow-y: auto;
}

/* ---------- Azioni ---------- */
.actions { display: flex; align-items: center; gap: 10px; margin-top: 10px; }
.act {
  display: inline-flex; align-items: center; gap: 5px; min-height: 30px; padding: 4px 8px;
  border: 0; border-radius: 8px; background: transparent; color: var(--muted); cursor: pointer; font-size: 0.8rem;
}
.act:hover { background: var(--surface-2); color: var(--text); }
.cost { font-size: 0.72rem; color: var(--faint); font-variant-numeric: tabular-nums; }

/* ---------- Avvisi ---------- */
.notice { padding: 8px 12px; border-radius: 10px; font-size: 0.88rem; overflow-wrap: anywhere; }
.notice.err { background: var(--err-bg); color: var(--err-text); border: 1px solid var(--err-border); }
.notice.info { color: var(--muted); font-style: italic; padding: 6px 0 0; }

/* ---------- Composer ---------- */
.composer-wrap { flex: none; padding: 8px 16px calc(10px + env(safe-area-inset-bottom)); background: var(--bg); }
.composer {
  max-width: var(--col); margin: 0 auto; display: flex; align-items: flex-end; gap: 8px;
  padding: 8px 8px 8px 16px; border: 1px solid var(--border-strong); border-radius: 22px;
  background: var(--surface); box-shadow: var(--shadow);
}
.composer:focus-within { border-color: var(--accent); }
.composer textarea {
  flex: 1; min-width: 0; resize: none; border: 0; outline: 0; background: transparent; color: var(--text);
  font: inherit; line-height: 24px; padding: 6px 0; max-height: calc(24px * 8 + 12px); overflow-y: hidden;
}
.composer textarea::placeholder { color: var(--faint); }
.composer textarea:focus-visible { outline: 0; }
.composer-side { flex: none; display: flex; align-items: center; gap: 8px; }
.count { font-size: 0.78rem; color: var(--muted); font-variant-numeric: tabular-nums; }
.count.over { color: var(--err-text); font-weight: 600; }
.btn-send {
  display: grid; place-items: center; width: 36px; height: 36px; border: 0; border-radius: 50%;
  background: var(--accent); color: var(--accent-text); cursor: pointer;
}
.btn-send:disabled { background: var(--surface-2); color: var(--faint); cursor: default; }
.btn-stop {
  display: inline-flex; align-items: center; gap: 6px; height: 36px; padding: 0 14px; border-radius: 18px;
  border: 1px solid var(--border-strong); background: var(--surface-2); cursor: pointer; font-size: 0.88rem; font-weight: 500;
}
.btn-stop:hover { border-color: var(--text); }
.disclaimer { max-width: var(--col); margin: 6px auto 0; text-align: center; font-size: 0.72rem; color: var(--faint); }

/* ---------- Schermi stretti ---------- */
@media (max-width: 600px) {
  .cards { grid-template-columns: minmax(0, 1fr); }
  .welcome { padding-top: 6vh; }
  .bubble { max-width: 92%; }
}
@media (max-width: 440px) {
  .brand-sub { display: none; }
}
