:root {
  --bg: #faf9f6;
  --surface: #ffffff;
  --sidebar: #f2f0ea;
  --text: #1f1d1a;
  --muted: #6b665e;
  --line: #e3dfd6;
  --accent: #c2410c;
  --accent-text: #ffffff;
  --accent-soft: #fbe9df;
  --user-bubble: #ece8df;
  --ok: #15803d;
  --ok-soft: #e3f4e8;
  --warn: #a16207;
  --warn-soft: #fdf3d7;
  --danger: #b91c1c;
  --danger-soft: #fde6e6;
  --code-bg: #f1eee7;
  --shadow: 0 10px 30px rgba(31, 29, 26, 0.12);
  --radius: 12px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1b1a18;
    --surface: #242320;
    --sidebar: #161513;
    --text: #ece9e2;
    --muted: #a39d92;
    --line: #37352f;
    --accent: #ea7a45;
    --accent-text: #1b1a18;
    --accent-soft: #3a2a21;
    --user-bubble: #2f2d29;
    --ok: #4ade80;
    --ok-soft: #1f3226;
    --warn: #facc15;
    --warn-soft: #3a3320;
    --danger: #f87171;
    --danger-soft: #3d2222;
    --code-bg: #2c2a26;
    --shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, input, textarea, select { font: inherit; color: inherit; }
a { color: var(--accent); }
.loading { padding: 40px; color: var(--muted); text-align: center; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.hidden { display: none !important; }

/* Pulsanti e campi */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--line); background: var(--surface); color: var(--text);
  padding: 8px 14px; border-radius: 9px; cursor: pointer; font-weight: 500; white-space: nowrap;
}
.btn:hover { border-color: var(--muted); }
a.btn { text-decoration: none; }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(1.06); }
.btn.danger { color: var(--danger); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--line); }
.btn.small { padding: 5px 10px; font-size: 13px; }
label.field { display: block; margin: 0 0 14px; }
label.field > span { display: block; font-size: 13px; font-weight: 600; margin-bottom: 5px; }
label.field > small { display: block; color: var(--muted); font-size: 12.5px; margin-top: 4px; }
input[type="text"], input[type="email"], input[type="password"], input[type="number"], textarea {
  width: 100%; padding: 9px 11px; border: 1px solid var(--line); border-radius: 9px;
  background: var(--bg); outline: none;
}
input:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
textarea { resize: vertical; min-height: 70px; }
.error { background: var(--danger-soft); color: var(--danger); padding: 9px 12px; border-radius: 9px; margin: 0 0 14px; font-size: 14px; }
.notice { background: var(--warn-soft); color: var(--text); padding: 9px 12px; border-radius: 9px; margin: 0 0 14px; font-size: 14px; border-left: 3px solid var(--warn); }
.success { background: var(--ok-soft); padding: 9px 12px; border-radius: 9px; margin: 0 0 14px; font-size: 14px; border-left: 3px solid var(--ok); }

/* Pagine di accesso */
.auth { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; }
.auth-card {
  width: 100%; max-width: 420px; background: var(--surface); border: 1px solid var(--line);
  border-radius: 16px; padding: 28px; box-shadow: var(--shadow);
}
.auth-card h1 { font-size: 22px; margin: 0 0 6px; }
.auth-card p.lead { color: var(--muted); margin: 0 0 20px; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; margin-bottom: 18px; }
.brand-dot { width: 26px; height: 26px; border-radius: 8px; background: var(--accent); display: grid; place-items: center; }
.brand-dot::after { content: ""; width: 10px; height: 10px; border-radius: 50%; background: var(--accent-text); }
.qr { background: #fff; padding: 10px; border-radius: 10px; width: 210px; margin: 6px auto 12px; }
.qr svg { display: block; width: 100%; height: auto; }
.secret { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 13px; word-break: break-all; background: var(--code-bg); padding: 8px 10px; border-radius: 8px; text-align: center; letter-spacing: 1px; }
.codes { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; font-family: ui-monospace, Menlo, Consolas, monospace; background: var(--code-bg); padding: 12px; border-radius: 10px; margin: 0 0 14px; text-align: center; }
.code-input { font-size: 22px; letter-spacing: 6px; text-align: center; font-family: ui-monospace, Menlo, Consolas, monospace; }

/* Struttura principale */
.shell { display: grid; grid-template-columns: 270px 1fr; height: 100vh; height: 100dvh; }
.sidebar { background: var(--sidebar); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.sidebar-head { padding: 14px 14px 10px; }
.sidebar-head .btn { width: 100%; }
.conv-list { flex: 1; overflow-y: auto; padding: 4px 8px 8px; min-height: 0; }
.conv-list h3 { font-size: 12px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin: 10px 8px 6px; }
.conv { display: flex; align-items: center; gap: 4px; border-radius: 8px; }
.conv button.open { flex: 1; text-align: left; background: none; border: 0; padding: 8px 10px; border-radius: 8px; cursor: pointer; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv:hover, .conv.active { background: var(--line); }
.conv button.del { visibility: hidden; background: none; border: 0; color: var(--muted); cursor: pointer; padding: 4px 8px; border-radius: 6px; }
.conv:hover button.del { visibility: visible; }
.conv button.del:hover { color: var(--danger); }
.sidebar-nav { border-top: 1px solid var(--line); padding: 8px; display: grid; gap: 2px; }
.sidebar-nav button { text-align: left; background: none; border: 0; padding: 8px 10px; border-radius: 8px; cursor: pointer; display: flex; justify-content: space-between; }
.sidebar-nav button:hover { background: var(--line); }
.sidebar-foot { padding: 10px 14px 14px; font-size: 13px; color: var(--muted); display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.sidebar-foot span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.topbar { display: flex; align-items: center; gap: 10px; padding: 10px 18px; border-bottom: 1px solid var(--line); background: var(--surface); flex-wrap: wrap; }
.topbar .menu { display: none; }
.status { display: flex; align-items: center; gap: 8px; flex: 1; min-width: 0; font-size: 14px; }
.status .dot { width: 9px; height: 9px; border-radius: 50%; background: var(--ok); flex: none; }
.status .dot.pending { background: var(--warn); }
.status .dot.busy { background: var(--accent); animation: pulse 1.2s infinite; }
.status-text { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
@keyframes pulse { 50% { opacity: 0.3; } }
.top-actions { display: flex; gap: 8px; flex-wrap: wrap; }

.chat { flex: 1; overflow-y: auto; min-height: 0; }
.thread { max-width: 780px; margin: 0 auto; padding: 24px 18px 30px; }
.welcome { text-align: center; padding: 40px 10px 10px; }
.welcome h2 { font-size: 26px; margin: 0 0 8px; font-weight: 600; }
.welcome p { color: var(--muted); margin: 0 auto 22px; max-width: 520px; }
.examples { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 10px; text-align: left; }
.examples button { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; cursor: pointer; text-align: left; }
.examples button:hover { border-color: var(--accent); }
.msg { margin: 0 0 22px; }
.msg.user { display: flex; justify-content: flex-end; }
.msg.user .bubble { background: var(--user-bubble); border-radius: 16px 16px 4px 16px; padding: 10px 14px; max-width: 85%; white-space: pre-wrap; word-wrap: break-word; }
.msg.assistant { display: flex; gap: 12px; }
.avatar { width: 28px; height: 28px; border-radius: 8px; background: var(--accent); flex: none; display: grid; place-items: center; margin-top: 2px; }
.avatar::after { content: ""; width: 10px; height: 10px; border-radius: 50%; background: var(--accent-text); }
.msg.assistant .body { min-width: 0; flex: 1; }
.msg.assistant.failed .md { color: var(--danger); }
.meta { font-size: 12.5px; color: var(--muted); margin-top: 6px; display: flex; gap: 12px; flex-wrap: wrap; }
.md p { margin: 0 0 10px; }
.md p:last-child { margin-bottom: 0; }
.md h1, .md h2, .md h3, .md h4 { font-size: 16px; margin: 14px 0 6px; }
.md ul, .md ol { margin: 0 0 10px; padding-left: 22px; }
.md li { margin: 2px 0; }
.md code { background: var(--code-bg); padding: 1px 5px; border-radius: 5px; font-size: 13.5px; font-family: ui-monospace, Menlo, Consolas, monospace; }
.md pre { background: var(--code-bg); padding: 10px 12px; border-radius: 9px; overflow-x: auto; }
.md pre code { background: none; padding: 0; }
.files { margin-top: 8px; font-size: 13px; }
.files summary { cursor: pointer; color: var(--muted); }
.files ul { margin: 6px 0 0; padding-left: 20px; font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 12.5px; }

.progress { border: 1px solid var(--line); background: var(--surface); border-radius: var(--radius); padding: 12px 14px; }
.progress-head { display: flex; align-items: center; gap: 10px; font-weight: 600; margin-bottom: 8px; }
.spinner { width: 16px; height: 16px; border-radius: 50%; border: 2px solid var(--line); border-top-color: var(--accent); animation: spin 0.8s linear infinite; flex: none; }
@keyframes spin { to { transform: rotate(360deg); } }
.steps { list-style: none; margin: 0; padding: 0; font-size: 13.5px; color: var(--muted); max-height: 220px; overflow-y: auto; }
.steps li { padding: 2px 0 2px 16px; position: relative; }
.steps li::before { content: "•"; position: absolute; left: 3px; }
.steps li:last-child { color: var(--text); }
.progress-foot { display: flex; justify-content: space-between; align-items: center; margin-top: 10px; font-size: 13px; color: var(--muted); gap: 8px; flex-wrap: wrap; }

.ready-card { border: 1px solid var(--accent); background: var(--accent-soft); border-radius: var(--radius); padding: 12px 14px; display: flex; gap: 12px; align-items: center; flex-wrap: wrap; margin: 0 0 22px 40px; }
.ready-card p { margin: 0; flex: 1; min-width: 200px; font-size: 14px; }

.composer { border-top: 1px solid var(--line); background: var(--bg); padding: 12px 18px 16px; }
.composer-box { max-width: 780px; margin: 0 auto; background: var(--surface); border: 1px solid var(--line); border-radius: 16px; padding: 8px 8px 8px 14px; display: flex; gap: 8px; align-items: flex-end; box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04); }
.composer-box:focus-within { border-color: var(--accent); }
.composer textarea { border: 0; background: transparent; box-shadow: none; padding: 6px 0; min-height: 26px; max-height: 240px; resize: none; }
.composer textarea:focus { box-shadow: none; }
.composer-hint { max-width: 780px; margin: 6px auto 0; font-size: 12px; color: var(--muted); text-align: center; }

/* Finestre */
dialog { border: 1px solid var(--line); border-radius: 16px; padding: 0; background: var(--surface); color: var(--text); box-shadow: var(--shadow); width: min(720px, calc(100vw - 32px)); max-height: calc(100vh - 48px); }
dialog::backdrop { background: rgba(0, 0, 0, 0.35); }
dialog.narrow { width: min(420px, calc(100vw - 32px)); }
.dlg-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px; border-bottom: 1px solid var(--line); position: sticky; top: 0; background: var(--surface); z-index: 1; }
.dlg-head h2 { margin: 0; font-size: 18px; }
.dlg-body { padding: 18px 20px 20px; }
.tabs { display: flex; gap: 4px; padding: 0 20px; border-bottom: 1px solid var(--line); overflow-x: auto; }
.tabs button { background: none; border: 0; padding: 10px 12px; cursor: pointer; border-bottom: 2px solid transparent; color: var(--muted); white-space: nowrap; }
.tabs button.active { color: var(--text); border-bottom-color: var(--accent); font-weight: 600; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 14px; }
.row-actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; margin-top: 6px; }
.list { list-style: none; margin: 0; padding: 0; }
.list li { padding: 10px 0; border-bottom: 1px solid var(--line); display: flex; gap: 12px; align-items: center; }
.list li:last-child { border-bottom: 0; }
.list .grow { flex: 1; min-width: 0; }
.list .title { font-weight: 500; overflow-wrap: anywhere; }
.badge { font-size: 12px; padding: 2px 8px; border-radius: 99px; background: var(--ok-soft); color: var(--ok); font-weight: 600; white-space: nowrap; }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.hash { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 12.5px; color: var(--muted); }
.section-title { font-size: 13px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); margin: 18px 0 6px; }
.section-title:first-child { margin-top: 0; }
.message-card { border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; margin-bottom: 10px; }
.message-card p { margin: 6px 0 0; white-space: pre-wrap; overflow-wrap: anywhere; }
.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; box-shadow: var(--shadow); z-index: 100; max-width: calc(100vw - 32px); }

@media (max-width: 820px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar { position: fixed; inset: 0 auto 0 0; width: 280px; z-index: 50; transform: translateX(-100%); transition: transform 0.2s; box-shadow: var(--shadow); }
  .shell.nav-open .sidebar { transform: none; }
  .topbar { padding: 10px 12px; }
  .topbar .menu { display: inline-flex; }
  .thread { padding: 18px 14px 24px; }
  .composer { padding: 10px 12px 12px; }
  .grid2 { grid-template-columns: 1fr; }
  .ready-card { margin-left: 0; }
  .top-actions .btn .lbl { display: none; }
}
.intro { margin-top: 0; }
.empty-list { padding: 0 8px; }
.success.spaced { margin-top: 14px; }
