:root {
  --bg: #f7f7fb;
  --surface: #ffffff;
  --surface-2: #f0f0f6;
  --border: #e4e4ee;
  --text: #16161d;
  --text-2: #5a5a6e;
  --text-3: #8d8da0;
  --accent: #5b4cf0;
  --accent-2: #8b5cf6;
  --accent-soft: #eeebfe;
  --accent-text: #4636d8;
  --ok: #0f9f6e;
  --ok-soft: #e3f6ee;
  --warn: #b45309;
  --warn-soft: #fdf1e1;
  --danger: #d0303b;
  --danger-soft: #fdebec;
  --shadow: 0 1px 2px rgba(20, 20, 40, .05), 0 4px 16px rgba(20, 20, 40, .06);
  --shadow-lg: 0 8px 40px rgba(20, 20, 40, .18);
  --r: 14px;
  --r-sm: 10px;
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e0e13; --surface: #17171f; --surface-2: #20202b; --border: #2a2a37;
    --text: #ececf3; --text-2: #a5a5b8; --text-3: #73738a;
    --accent: #7c6cff; --accent-2: #a78bfa; --accent-soft: #24203f; --accent-text: #b4a9ff;
    --ok: #34d399; --ok-soft: #10291f; --warn: #f5b041; --warn-soft: #2d2311;
    --danger: #ff6b74; --danger-soft: #331418;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 4px 16px rgba(0,0,0,.25);
    --shadow-lg: 0 8px 40px rgba(0,0,0,.6);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0e0e13; --surface: #17171f; --surface-2: #20202b; --border: #2a2a37;
  --text: #ececf3; --text-2: #a5a5b8; --text-3: #73738a;
  --accent: #7c6cff; --accent-2: #a78bfa; --accent-soft: #24203f; --accent-text: #b4a9ff;
  --ok: #34d399; --ok-soft: #10291f; --warn: #f5b041; --warn-soft: #2d2311;
  --danger: #ff6b74; --danger-soft: #331418;
  --shadow: 0 1px 2px rgba(0,0,0,.3), 0 4px 16px rgba(0,0,0,.25);
  --shadow-lg: 0 8px 40px rgba(0,0,0,.6);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--text); font: 14px/1.5 var(--font);
  -webkit-font-smoothing: antialiased; overflow: hidden;
}
button { font: inherit; color: inherit; cursor: pointer; }
button:disabled { cursor: default; }
a { color: var(--accent-text); }
svg { display: block; }
[hidden] { display: none !important; }

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

/* ---------- testata ---------- */
.top {
  display: flex; align-items: center; gap: 10px; padding: 10px 10px 10px 14px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.fav-wrap { width: 28px; height: 28px; flex: none; border-radius: 8px; background: var(--surface-2); display: grid; place-items: center; overflow: hidden; }
.fav { width: 18px; height: 18px; }
.fav-fallback { font-weight: 700; font-size: 13px; color: var(--text-2); }
.titles { flex: 1; min-width: 0; }
.title { font-weight: 600; font-size: 13.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.meta { display: flex; align-items: center; gap: 6px; min-width: 0; margin-top: 1px; }
.meta-text { font-size: 11.5px; color: var(--text-3); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.badge {
  flex: none; font-size: 10.5px; font-weight: 600; letter-spacing: .02em; padding: 1px 7px; border-radius: 99px;
  background: var(--ok-soft); color: var(--ok);
}
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.tools { display: flex; gap: 2px; flex: none; }
.icon-btn {
  width: 32px; height: 32px; display: grid; place-items: center; border: 0; border-radius: 9px;
  background: transparent; color: var(--text-2); transition: background .15s, color .15s;
}
.icon-btn:hover:not(:disabled) { background: var(--surface-2); color: var(--text); }
.icon-btn:disabled { opacity: .35; }
.icon-btn svg { width: 18px; height: 18px; }

/* ---------- area centrale ---------- */
.scroll { flex: 1; overflow-y: auto; overflow-x: hidden; padding: 16px 14px 24px; scroll-behavior: smooth; }
.scroll::-webkit-scrollbar { width: 8px; }
.scroll::-webkit-scrollbar-thumb { background: var(--border); border-radius: 8px; }

.empty { display: flex; flex-direction: column; gap: 14px; animation: rise .35s ease both; }
.hero {
  display: flex; gap: 12px; align-items: center; padding: 12px; border-radius: var(--r);
  background: var(--surface); box-shadow: var(--shadow); border: 1px solid var(--border);
}
.hero-thumb {
  width: 112px; aspect-ratio: 16/9; flex: none; border-radius: 9px; background: var(--surface-2) center/cover no-repeat;
  position: relative;
}
.hero-thumb::after {
  content: ""; position: absolute; inset: 0; margin: auto; width: 30px; height: 30px; border-radius: 50%;
  background: rgba(0,0,0,.55) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='white'%3E%3Cpath d='M9 7l8 5-8 5z'/%3E%3C/svg%3E") center/18px no-repeat;
}
.hero-body { min-width: 0; flex: 1; }
.hero-kind { font-size: 10.5px; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; color: var(--accent-text); }
.hero-title { font-weight: 600; font-size: 14px; line-height: 1.35; margin-top: 2px; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.hero-host { font-size: 12px; color: var(--text-3); margin-top: 3px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.blocked { text-align: center; color: var(--text-2); padding: 28px 12px; }
.blocked-icon { width: 44px; height: 44px; margin: 0 auto 10px; color: var(--text-3); }
.blocked-icon svg { width: 44px; height: 44px; }

.section-label { font-size: 11px; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; color: var(--text-3); margin: 6px 2px 8px; }
.actions { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 8px; }
.action {
  text-align: left; border: 1px solid var(--border); background: var(--surface); border-radius: var(--r);
  padding: 12px; display: flex; flex-direction: column; gap: 8px; box-shadow: var(--shadow);
  transition: transform .15s, border-color .15s, box-shadow .15s;
}
.action:hover:not(:disabled) { transform: translateY(-1px); border-color: var(--accent); box-shadow: 0 6px 20px rgba(91, 76, 240, .15); }
.action:active:not(:disabled) { transform: translateY(0); }
.action:disabled { opacity: .5; }
.action-icon { width: 30px; height: 30px; border-radius: 9px; display: grid; place-items: center; background: var(--accent-soft); color: var(--accent-text); }
.action-icon svg { width: 17px; height: 17px; }
.action.primary { background: linear-gradient(135deg, var(--accent), var(--accent-2)); border-color: transparent; color: #fff; }
.action.primary .action-icon { background: rgba(255,255,255,.2); color: #fff; }
.action.primary .action-hint { color: rgba(255,255,255,.8); }
.action-title { font-weight: 600; font-size: 13.5px; line-height: 1.2; }
.action-hint { font-size: 11.5px; color: var(--text-3); margin-top: 1px; }

.suggestions { display: flex; flex-direction: column; gap: 6px; }
.chip {
  text-align: left; border: 1px solid var(--border); background: transparent; border-radius: 99px; padding: 7px 12px;
  font-size: 12.5px; color: var(--text-2); display: flex; align-items: center; gap: 8px; transition: background .15s, color .15s;
}
.chip:hover:not(:disabled) { background: var(--surface); color: var(--text); border-color: var(--accent); }
.chip svg { width: 14px; height: 14px; flex: none; color: var(--accent-text); }

/* ---------- conversazione ---------- */
.thread { display: flex; flex-direction: column; gap: 14px; }
.msg-user {
  align-self: flex-end; max-width: 88%; background: var(--accent); color: #fff; padding: 8px 12px;
  border-radius: 16px 16px 4px 16px; white-space: pre-wrap; word-wrap: break-word; font-size: 13.5px;
  animation: rise .2s ease both;
}
.msg-user.is-action { display: inline-flex; align-items: center; gap: 6px; font-weight: 600; }
.msg-user.is-action svg { width: 14px; height: 14px; }
.msg-model {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--r); padding: 12px 14px 8px;
  box-shadow: var(--shadow); min-width: 0; animation: rise .25s ease both;
}
.msg-model.error { border-color: color-mix(in srgb, var(--danger) 40%, transparent); background: var(--danger-soft); }
.msg-model.error .md { color: var(--danger); }

.pending { display: flex; flex-direction: column; gap: 9px; padding: 4px 0 8px; }
.pending-step { display: flex; align-items: center; gap: 8px; font-size: 12.5px; color: var(--text-2); }
.pending-step .elapsed { margin-left: auto; color: var(--text-3); font-variant-numeric: tabular-nums; }
.dots { display: inline-flex; gap: 3px; }
.dots i { width: 5px; height: 5px; border-radius: 50%; background: var(--accent); animation: blink 1.2s infinite ease-in-out; }
.dots i:nth-child(2) { animation-delay: .15s; }
.dots i:nth-child(3) { animation-delay: .3s; }
.skel { height: 9px; border-radius: 5px; background: linear-gradient(90deg, var(--surface-2) 25%, var(--border) 50%, var(--surface-2) 75%); background-size: 200% 100%; animation: shimmer 1.4s infinite linear; }
.skel:nth-child(3) { width: 92%; } .skel:nth-child(4) { width: 76%; } .skel:nth-child(5) { width: 84%; }

.msg-foot { display: flex; align-items: center; gap: 2px; margin-top: 6px; padding-top: 6px; border-top: 1px solid var(--border); }
.msg-foot .icon-btn { width: 28px; height: 28px; }
.msg-foot .icon-btn svg { width: 15px; height: 15px; }
.msg-foot .foot-note { margin-left: auto; font-size: 11px; color: var(--text-3); }
.retry { margin-top: 8px; }
.caret { display: inline-block; width: 7px; height: 14px; margin-left: 2px; vertical-align: -2px; background: var(--accent); border-radius: 2px; animation: blink 1s infinite; }

.sources { margin: 8px 0 0; padding: 8px 0 0 0; border-top: 1px dashed var(--border); list-style: none; display: flex; flex-direction: column; gap: 4px; counter-reset: s; }
.sources li { counter-increment: s; font-size: 12px; display: flex; gap: 6px; min-width: 0; }
.sources li::before { content: counter(s); flex: none; width: 17px; height: 17px; border-radius: 5px; background: var(--surface-2); color: var(--text-2); font-size: 10px; font-weight: 700; display: grid; place-items: center; }
.sources a { color: var(--text-2); text-decoration: none; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sources a:hover { color: var(--accent-text); text-decoration: underline; }

/* ---------- markdown ---------- */
.md { font-size: 13.5px; line-height: 1.6; word-wrap: break-word; overflow-wrap: anywhere; }
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p { margin: 0 0 .7em; }
.md h1, .md h2, .md h3, .md h4 { font-size: 14px; margin: 1.1em 0 .4em; line-height: 1.3; }
.md h1, .md h2 { font-size: 15px; }
.md strong { font-weight: 650; color: var(--text); }
.md ul, .md ol { margin: 0 0 .8em; padding-left: 1.3em; }
.md li { margin: .25em 0; }
.md li::marker { color: var(--accent-text); }
.md blockquote { margin: .6em 0; padding: .3em .9em; border-left: 3px solid var(--accent); background: var(--accent-soft); border-radius: 0 8px 8px 0; color: var(--text-2); }
.md blockquote p { margin: .2em 0; }
.md code { font: 12px var(--mono); background: var(--surface-2); padding: .1em .35em; border-radius: 5px; }
.md pre { background: var(--surface-2); padding: 10px 12px; border-radius: 9px; overflow-x: auto; }
.md pre code { background: none; padding: 0; }
.md table { border-collapse: collapse; width: 100%; font-size: 12.5px; margin: .6em 0; display: block; overflow-x: auto; }
.md th, .md td { border: 1px solid var(--border); padding: 5px 8px; text-align: left; }
.md th { background: var(--surface-2); }
.md hr { border: 0; border-top: 1px solid var(--border); margin: 1em 0; }
.md a { text-decoration: none; border-bottom: 1px solid color-mix(in srgb, var(--accent-text) 40%, transparent); }
.ts {
  display: inline-flex; align-items: center; gap: 3px; border: 0; padding: 0 6px; margin: 0 1px; border-radius: 6px;
  background: var(--accent-soft); color: var(--accent-text); font: 600 11.5px/1.7 var(--mono); vertical-align: 1px;
}
.ts:hover { background: var(--accent); color: #fff; }
.ts svg { width: 9px; height: 9px; }

/* ---------- composer ---------- */
.composer-wrap { padding: 10px 12px 12px; background: var(--bg); }
.composer {
  display: flex; align-items: flex-end; gap: 6px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 16px; padding: 6px 6px 6px 12px; box-shadow: var(--shadow); transition: border-color .15s, box-shadow .15s;
}
.composer:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent); }
.composer textarea {
  flex: 1; border: 0; outline: 0; resize: none; background: transparent; color: var(--text); font: 13.5px/1.45 var(--font);
  padding: 6px 0; max-height: 160px; min-height: 22px;
}
.composer textarea::placeholder { color: var(--text-3); }
.send {
  width: 34px; height: 34px; flex: none; border: 0; border-radius: 11px; display: grid; place-items: center;
  background: var(--accent); color: #fff; transition: opacity .15s, transform .1s;
}
.send svg { width: 17px; height: 17px; }
.send:disabled { opacity: .3; }
.send:active:not(:disabled) { transform: scale(.94); }
.send.stop { background: var(--text); color: var(--bg); }
.composer-bar { display: flex; align-items: center; gap: 8px; margin-top: 8px; }
.seg { display: inline-flex; padding: 2px; border-radius: 10px; background: var(--surface-2); gap: 2px; }
.seg-btn { border: 0; background: transparent; padding: 4px 10px; border-radius: 8px; font-size: 12px; font-weight: 550; color: var(--text-2); transition: background .15s, color .15s; }
.seg-btn.active { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.12); }
#mode-web.active { color: var(--accent-text); }
.seg-full { display: flex; width: 100%; }
.seg-full .seg-btn { flex: 1; padding: 6px 8px; }
.model-chip { margin-left: auto; border: 0; background: transparent; font-size: 11.5px; color: var(--text-3); padding: 4px 6px; border-radius: 7px; max-width: 50%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.model-chip:hover { background: var(--surface-2); color: var(--text-2); }

/* ---------- fogli: impostazioni e benvenuto ---------- */
.sheet { position: fixed; inset: 0; background: rgba(10, 10, 20, .35); backdrop-filter: blur(3px); display: flex; align-items: flex-end; z-index: 10; animation: fade .2s ease both; }
.sheet-card {
  width: 100%; max-height: 92%; overflow-y: auto; background: var(--surface); border-radius: 20px 20px 0 0; padding: 16px 16px 18px;
  box-shadow: var(--shadow-lg); display: flex; flex-direction: column; gap: 14px; animation: up .25s cubic-bezier(.2,.8,.2,1) both;
}
.sheet-head { display: flex; align-items: center; justify-content: space-between; }
.sheet-head h1 { font-size: 17px; margin: 0; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field-label { font-size: 12px; font-weight: 650; color: var(--text-2); }
.field-help { font-size: 11.5px; color: var(--text-3); line-height: 1.45; }
.field input, .field select, .onboarding input {
  width: 100%; border: 1px solid var(--border); background: var(--bg); color: var(--text); border-radius: var(--r-sm);
  padding: 9px 11px; font: 13px var(--font); outline: 0;
}
.field input:focus, .field select:focus, .onboarding input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent); }
.key-row { display: flex; gap: 6px; align-items: center; }
.key-row input { flex: 1; font-family: var(--mono); font-size: 12.5px; }
.status { margin: -6px 0 0; font-size: 12px; padding: 7px 10px; border-radius: 8px; background: var(--surface-2); color: var(--text-2); }
.status.ok { background: var(--ok-soft); color: var(--ok); }
.status.err { background: var(--danger-soft); color: var(--danger); }
.shortcut kbd { font: 600 10.5px var(--mono); padding: 1px 5px; border: 1px solid var(--border); border-bottom-width: 2px; border-radius: 5px; background: var(--bg); }
.sheet-foot { display: flex; justify-content: flex-end; gap: 8px; padding-top: 4px; }
.btn { border: 1px solid transparent; border-radius: var(--r-sm); padding: 8px 14px; font-size: 13px; font-weight: 600; transition: background .15s, opacity .15s; }
.btn:disabled { opacity: .45; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover:not(:disabled) { background: color-mix(in srgb, var(--accent) 88%, black); }
.btn-ghost { background: transparent; border-color: var(--border); color: var(--text); }
.btn-ghost:hover:not(:disabled) { background: var(--surface-2); }
.btn-block { width: 100%; padding: 11px; }
.btn-sm { padding: 5px 10px; font-size: 12px; }

.onboarding { position: fixed; inset: 0; z-index: 20; background: var(--bg); display: flex; align-items: center; justify-content: center; padding: 20px; overflow-y: auto; }
.ob-card { width: 100%; max-width: 380px; display: flex; flex-direction: column; gap: 14px; animation: rise .35s ease both; }
.ob-logo { width: 56px; height: 56px; border-radius: 14px; box-shadow: var(--shadow); }
.ob-card h1 { margin: 0; font-size: 21px; letter-spacing: -.01em; }
.ob-sub { margin: -6px 0 0; color: var(--text-2); }
.ob-steps { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.ob-steps li { display: flex; align-items: center; gap: 10px; font-size: 13px; }
.ob-steps span { width: 22px; height: 22px; flex: none; border-radius: 50%; display: grid; place-items: center; background: var(--accent-soft); color: var(--accent-text); font-size: 11.5px; font-weight: 700; }

.toast {
  position: fixed; left: 50%; bottom: 110px; transform: translate(-50%, 12px); opacity: 0; pointer-events: none;
  background: var(--text); color: var(--bg); padding: 7px 14px; border-radius: 99px; font-size: 12.5px; font-weight: 550;
  transition: opacity .2s, transform .2s; z-index: 30; box-shadow: var(--shadow-lg); white-space: nowrap;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@keyframes rise { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes up { from { transform: translateY(30px); opacity: .6; } to { transform: none; opacity: 1; } }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }
@keyframes shimmer { from { background-position: 200% 0; } to { background-position: -200% 0; } }
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation: none !important; transition: none !important; } }
