:root {
  --bg: #f6f5f1; --panel: #ffffff; --side: #efede6; --text: #1d2321; --muted: #6a716d;
  --line: #dedbd2; --accent: #2f5d50; --accent-ink: #ffffff; --accent-soft: #e3ece8;
  --warn: #8a5a00; --warn-bg: #fff4dc; --err: #a33a2c; --chip: #e7e3d8;
  --radius: 12px; --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 16px rgba(0,0,0,.04);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #151816; --panel: #1c201e; --side: #121412; --text: #e7e9e6; --muted: #9aa29d;
    --line: #2d332f; --accent: #6fb39b; --accent-ink: #0f1311; --accent-soft: #1f2e28;
    --warn: #e8b85a; --warn-bg: #2d2615; --err: #ff8a7a; --chip: #2a2f2c;
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font: 15px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
[hidden] { display: none !important; }
button { font: inherit; cursor: pointer; }
.muted { color: var(--muted); }
.error { color: var(--err); min-height: 1.2em; margin: 0; }

.primary { background: var(--accent); color: var(--accent-ink); border: 0; border-radius: 9px; padding: .6em 1.1em; font-weight: 600; }
.primary:disabled { opacity: .5; cursor: default; }
.primary.small { padding: .4em .8em; font-size: .9em; }
.link { background: none; border: 0; color: var(--accent); padding: 0; }
.icon { background: none; border: 0; color: var(--text); font-size: 1.3em; padding: .2em .4em; }

.brand { display: flex; align-items: center; gap: .5em; font-weight: 700; font-size: 1.1em; }
.logo { display: inline-grid; place-items: center; width: 1.7em; height: 1.7em; border-radius: 7px; background: var(--accent); color: var(--accent-ink); font-size: .9em; }

/* Login */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card { width: min(380px, 100%); background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 28px; display: grid; gap: 14px; }
.login-card label { display: grid; gap: 4px; font-size: .9em; color: var(--muted); }
input, textarea { font: inherit; color: var(--text); background: var(--bg); border: 1px solid var(--line); border-radius: 9px; padding: .55em .7em; }
input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

/* Layout */
.app { display: grid; grid-template-columns: 270px 1fr; height: 100%; }
.sidebar { background: var(--side); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.side-head { display: flex; justify-content: space-between; align-items: center; padding: 14px; }
.conv-list { flex: 1; overflow-y: auto; padding: 0 8px; }
.conv { display: flex; align-items: center; gap: 4px; border-radius: 8px; padding: 2px 4px 2px 10px; }
.conv:hover, .conv.active { background: var(--accent-soft); }
.conv a { flex: 1; color: var(--text); text-decoration: none; padding: 7px 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: .93em; }
.conv .actions { display: none; }
.conv:hover .actions, .conv.active .actions { display: flex; }
.conv .actions button { background: none; border: 0; color: var(--muted); padding: 4px; font-size: .85em; }
.side-foot { display: flex; justify-content: space-between; padding: 12px 14px; border-top: 1px solid var(--line); font-size: .9em; }

.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.topbar { display: flex; align-items: center; gap: 8px; padding: 10px 16px; border-bottom: 1px solid var(--line); }
.topbar h1 { font-size: 1em; margin: 0; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; flex: 1; }
#model { font: inherit; font-size: .85em; color: var(--text); background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: .3em .5em; max-width: 45%; }
.ext-warn { background: var(--warn-bg); color: var(--warn); font-size: .85em; padding: 6px 16px; border-bottom: 1px solid var(--line); }
#menu { display: none; }

.messages { flex: 1; overflow-y: auto; padding: 24px 16px; }
.messages > * { max-width: 820px; margin-left: auto; margin-right: auto; }
.empty { text-align: center; margin-top: 8vh; }
.empty h2 { margin: 0 0 .3em; }
.examples { display: grid; gap: 8px; margin-top: 22px; }
.example { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: .7em 1em; color: var(--text); text-align: left; }
.example:hover { border-color: var(--accent); }

.msg { margin-bottom: 22px; }
.msg.user .bubble { background: var(--accent-soft); border-radius: var(--radius); padding: 10px 14px; margin-left: 15%; white-space: pre-wrap; }
.msg.user .meta { text-align: right; }
.msg.assistant .bubble { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 18px; box-shadow: var(--shadow); }
.meta { font-size: .8em; color: var(--muted); margin-top: 4px; }
.bubble h3 { font-size: 1em; margin: 1em 0 .3em; }
.bubble h3:first-child { margin-top: 0; }
.bubble p { margin: .5em 0; }
.bubble ul, .bubble ol { margin: .4em 0; padding-left: 1.4em; }
.bubble em { color: var(--muted); font-style: normal; font-size: .88em; }
.cite { display: inline-block; background: var(--chip); color: var(--text); border: 0; border-radius: 6px; padding: 0 .35em; font-size: .78em; font-weight: 600; vertical-align: 1px; margin: 0 1px; }
.cite:hover { background: var(--accent); color: var(--accent-ink); }
.warn { background: var(--warn-bg); color: var(--warn); border-radius: 8px; padding: 8px 12px; margin-top: 10px; font-size: .9em; }

.progress { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: .93em; }
.spinner { width: 16px; height: 16px; border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Fonti */
.sources { margin-top: 12px; }
.sources summary { cursor: pointer; color: var(--muted); font-size: .9em; }
.src { border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; margin-top: 8px; background: var(--panel); scroll-margin: 80px; }
.src.flash { outline: 2px solid var(--accent); }
.src-head { display: flex; gap: 8px; align-items: baseline; flex-wrap: wrap; }
.src-n { font-weight: 700; color: var(--accent); }
.src-label { font-weight: 600; }
.src-vig { font-size: .82em; color: var(--muted); }
.src a.open { margin-left: auto; font-size: .85em; color: var(--accent); white-space: nowrap; }
.src-text { font-size: .86em; color: var(--muted); margin: 6px 0 0; white-space: pre-wrap; max-height: 7.5em; overflow: hidden; }
.src.open-text .src-text { max-height: none; }
.src-more { font-size: .82em; }
.note { font-size: .82em; border-left: 3px solid var(--line); padding-left: 8px; margin-top: 6px; color: var(--muted); }

.composer { border-top: 1px solid var(--line); padding: 12px 16px 10px; background: var(--bg); }
.composer > * { max-width: 820px; margin-left: auto; margin-right: auto; }
.composer textarea { display: block; width: 100%; resize: none; max-height: 40vh; }
.composer-row { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-top: 8px; }
.date-label { font-size: .85em; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.date-label input { padding: .3em .5em; }
.disclaimer { font-size: .75em; margin-top: 6px; text-align: center; }

.scrim { display: none; }
@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { position: fixed; inset: 0 auto 0 0; width: 84%; max-width: 320px; z-index: 20; transform: translateX(-100%); transition: transform .2s; }
  .sidebar.open { transform: none; }
  .scrim:not([hidden]) { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 10; }
  #menu { display: inline-block; }
  .msg.user .bubble { margin-left: 8%; }
  .date-label { font-size: .8em; }
}
.note.conc { border-left-color: var(--accent); color: var(--text); background: var(--accent-soft); padding: 4px 8px; border-radius: 0 6px 6px 0; }

/* approfondimento */
.msg.assistant .deep { display: block; margin: .35rem 0 0 .2rem; font-size: .85rem; }
.verif { margin-left: .4rem; font-size: .75rem; padding: .05rem .4rem; border-radius: .6rem;
  background: var(--accent-soft); color: var(--accent); white-space: nowrap; }
