/* farinata — stile WhatsApp Web dark. Nessuna risorsa esterna. */

:root {
  --bg:        #111b21;
  --sidebar:   #202c33;
  --accent:    #00a884;
  --accent-hi: #06cf9c;
  --out:       #005c4b;
  --in:        #202c33;
  --text:      #e9edef;
  --text-2:    #8696a0;
  --input:     #2a3942;
  --border:    #2f3b43;
  --danger:    #f15c6d;
  --warn:      #ffd279;
  --read:      #53bdeb;
  --thread-bg: #0b141a;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

html, body { height: 100%; }

body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
}

button { font: inherit; color: inherit; background: none; border: none; cursor: pointer; }
input, textarea, select {
  font: inherit; color: var(--text);
  background: var(--input);
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 8px 12px;
  outline: none;
  width: 100%;
}
input:focus, textarea:focus, select:focus { border-color: var(--accent); }
textarea { resize: vertical; }
code { font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; font-size: 12.5px; }
a { color: var(--read); }

.hidden { display: none !important; }
.muted { color: var(--text-2); font-size: 13px; }

/* ---------- banner config ---------- */
.banner {
  position: fixed; top: 0; left: 0; right: 0; z-index: 60;
  background: var(--warn); color: #1f2937;
  padding: 8px 16px; text-align: center; font-size: 13.5px;
}

/* ---------- toast ---------- */
.toasts {
  position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%);
  z-index: 70; display: flex; flex-direction: column; gap: 8px; align-items: center;
  pointer-events: none;
}
.toast {
  background: #7f1d28; color: #fff;
  border: 1px solid var(--danger);
  padding: 10px 18px; border-radius: 8px;
  max-width: 560px; font-size: 13px;
  box-shadow: 0 4px 16px rgba(0,0,0,.5);
  animation: toast-in .18s ease-out;
}
.toast.ok { background: #054d40; border-color: var(--accent); }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }

/* ---------- layout ---------- */
.app { display: flex; height: 100vh; }
body.with-banner .app { height: calc(100vh - 34px); margin-top: 34px; }
body.with-banner .sidebar { top: 34px; }

/* ---------- sidebar ---------- */
.sidebar {
  width: 30%; min-width: 320px; max-width: 430px;
  background: var(--sidebar);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  z-index: 40;
}
.sb-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 14px;
  background: var(--sidebar);
  border-bottom: 1px solid var(--border);
  min-height: 56px;
}
.brand { font-size: 17px; font-weight: 600; letter-spacing: .3px; white-space: nowrap; }
.brand-logo { margin-right: 2px; }
.tabs { display: flex; gap: 2px; }
.tab {
  font-size: 17px; padding: 6px 8px; border-radius: 8px;
  filter: grayscale(60%); opacity: .75;
  transition: background .12s, opacity .12s;
}
.tab:hover { background: var(--input); opacity: 1; }
.tab.active { background: var(--input); filter: none; opacity: 1; box-shadow: inset 0 -2px 0 var(--accent); }

.newchat, .search { padding: 8px 12px; display: flex; gap: 8px; }
.newchat { padding-bottom: 0; }
.newchat input, .search input { background: var(--input); font-size: 13.5px; }
.icon-btn {
  font-size: 17px; padding: 6px 10px; border-radius: 8px;
  color: var(--text-2); flex-shrink: 0;
}
.icon-btn:hover { background: var(--input); color: var(--text); }

/* lista chat */
.chat-list { flex: 1; overflow-y: auto; margin-top: 4px; }
.chat-item {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 14px; cursor: pointer;
  border-bottom: 1px solid rgba(134,150,160,.08);
}
.chat-item:hover { background: var(--input); }
.chat-item.active { background: #2a3942; }
.chat-item .avatar { width: 44px; height: 44px; font-size: 18px; }
.ci-main { flex: 1; min-width: 0; }
.ci-top { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.ci-name { font-size: 15px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ci-time { font-size: 11.5px; color: var(--text-2); flex-shrink: 0; }
.ci-bottom { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-top: 2px; }
.ci-excerpt {
  font-size: 13px; color: var(--text-2);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1;
}
.ci-badge {
  background: var(--accent); color: #111b21;
  font-size: 11.5px; font-weight: 700;
  border-radius: 12px; padding: 1px 7px; flex-shrink: 0;
}
.chat-list-empty { padding: 24px 16px; text-align: center; color: var(--text-2); font-size: 13px; }

/* footer discreto con versione */
.sb-foot {
  display: flex; align-items: baseline; justify-content: space-between;
  padding: 7px 14px; border-top: 1px solid var(--border);
  font-size: 11px; color: var(--text-2); opacity: .6;
  user-select: none;
}
.sb-foot-name { letter-spacing: .2px; }
.sb-foot-ver { font-variant-numeric: tabular-nums; }

/* avatar */
.avatar {
  width: 40px; height: 40px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 16px; font-weight: 600; color: #e9edef;
  background: #4a5d69; flex-shrink: 0; user-select: none;
}

/* ---------- pannello destro ---------- */
.panel { flex: 1; min-width: 0; position: relative; display: flex; flex-direction: column; }
.sb-toggle {
  display: none;
  position: absolute; top: 8px; left: 8px; z-index: 45;
  font-size: 18px; padding: 6px 10px; border-radius: 8px;
  background: var(--sidebar); color: var(--text);
  box-shadow: 0 2px 8px rgba(0,0,0,.4);
}

.view { display: none; flex: 1; min-height: 0; flex-direction: column; }
.view.active { display: flex; }

.view-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 20px; min-height: 56px;
  background: var(--sidebar); border-bottom: 1px solid var(--border);
}
.view-head h2 { font-size: 16px; font-weight: 600; }
.view-body { flex: 1; overflow-y: auto; padding: 18px 20px; }
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; align-items: start; }
.col { display: flex; flex-direction: column; gap: 18px; min-width: 0; }

.card {
  background: var(--sidebar);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px;
  display: flex; flex-direction: column; gap: 10px;
}
.card h3 { font-size: 14.5px; font-weight: 600; color: var(--accent-hi); }
.card label { display: flex; flex-direction: column; gap: 4px; font-size: 12.5px; color: var(--text-2); }
.card label.check { flex-direction: row; align-items: center; gap: 8px; font-size: 13.5px; color: var(--text); cursor: pointer; }
.card label.check input { accent-color: var(--accent); width: 16px; height: 16px; }

/* profili endpoint LLM: ogni profilo è un blocco su due righe, il riquadro
   fa da separatore (regge nomi/modelli di qualsiasi lunghezza) */
.prof-head { display: flex; align-items: center; justify-content: space-between; margin: 6px 0 4px; font-size: 12.5px; color: var(--text-2); }
.prof-list { display: flex; flex-direction: column; gap: 8px; }
.prof-item {
  display: flex; flex-direction: column; gap: 6px;
  border: 1px solid var(--border); border-radius: 8px;
  padding: 8px; background: rgba(255,255,255,.02);
}
.prof-r1 { display: flex; gap: 6px; align-items: center; }
.prof-r1 input { flex: 1; min-width: 0; }         /* nome */
.prof-r1 select { width: 96px; flex-shrink: 0; }  /* tipo */
.prof-r1 button { flex-shrink: 0; }
.prof-r2 { display: flex; }
.prof-r2 input { flex: 1; min-width: 0; }          /* URL a tutta larghezza */
.prof-item input, .prof-item select { font-size: 12px; padding: 6px 7px; }

/* instradamento per modalità: [modalità] [motore] [modello] */
.route-list { display: flex; flex-direction: column; gap: 6px; }
/* etichetta modalità (con emoji della capacità) a filo del box; motore giusto
   per "Unsloth GPU"; il resto della larghezza va al modello. */
.route-row { display: grid; grid-template-columns: 92px 118px 1fr; gap: 6px; align-items: center; }
.route-mod { font-size: 12.5px; color: var(--text-2); white-space: nowrap; text-align: right; }
.route-row select { font-size: 12px; padding: 6px 7px; min-width: 0; }
.route-hint { font-size: 11px; color: var(--text-2); opacity: .85; line-height: 1.3; margin: -2px 0 3px 98px; }
.route-hint.inactive { font-style: italic; opacity: .7; }
.tier-list { margin: 5px 0; border: 1px solid var(--border, #2a3942); border-radius: 6px; overflow: hidden; }
.tier-row { display: flex; justify-content: space-between; font-size: 12px; padding: 3px 8px; color: var(--text-2); }
.tier-row + .tier-row { border-top: 1px solid var(--border, #2a3942); }
.tier-current { font-weight: 600; color: var(--text); background: rgba(37,211,102,.10); }
/* multi-numero: barra selettore + gestione numeri */
.acct-bar { padding: 6px 10px; }
.acct-bar select { width: 100%; font-size: 13px; padding: 6px 8px; }
.acct-list { display: flex; flex-direction: column; gap: 5px; margin: 6px 0; }
.acct-row { display: flex; align-items: center; gap: 6px; }
.acct-row .acct-lbl { font-weight: 600; }
.acct-row .acct-num { color: var(--text-2); font-size: 12px; margin-right: auto; }
.acct-editor { margin-top: 8px; border-top: 1px solid var(--border, #2a3942); padding-top: 8px; }
#calls-status > div { margin: 2px 0; }
@media (max-width: 620px) { .route-row { grid-template-columns: 1fr 1fr; } .route-mod { grid-column: 1 / -1; text-align: left; } .route-hint { margin-left: 0; } }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.btn-row { display: flex; gap: 10px; }

.btn {
  background: var(--input); border-radius: 8px;
  padding: 8px 16px; font-size: 13.5px;
  transition: filter .12s;
}
.btn:hover { filter: brightness(1.15); }
.btn:disabled { opacity: .45; cursor: default; pointer-events: none; }
.btn.primary { background: var(--accent); color: #111b21; font-weight: 600; }
.btn.ghost { background: transparent; border: 1px solid var(--border); color: var(--text-2); }
.btn.ghost:hover { color: var(--text); }
.btn.small { padding: 4px 10px; font-size: 12px; }
.btn.danger { color: var(--danger); }

/* ---------- tab chat ---------- */
.chat-empty {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 10px;
  color: var(--text-2); text-align: center;
  border-bottom: 5px solid var(--accent);
}
.empty-logo { font-size: 64px; filter: grayscale(30%); }
.chat-empty h2 { color: var(--text); font-weight: 300; font-size: 28px; }
.chat-empty p { font-size: 13.5px; line-height: 1.7; }

.thread { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.thread-head {
  display: flex; align-items: center; gap: 12px;
  padding: 9px 16px; min-height: 56px;
  background: var(--sidebar); border-bottom: 1px solid var(--border);
}
.thread-name { font-size: 15px; font-weight: 500; }
.thread-waid { font-size: 12px; color: var(--text-2); }
.thread-id { flex: 1; min-width: 0; }

/* deroga bot della singola chat */
.thread-bot { display: flex; align-items: center; gap: 6px; flex-shrink: 0; }
.thread-bot select { font-size: 12.5px; padding: 5px 8px; max-width: 180px; }
.thread-bot-hint { font-size: 11px; color: var(--text-2); white-space: nowrap; }
@media (max-width: 720px) { .thread-bot-hint { display: none; } }

.thread-msgs {
  flex: 1; overflow-y: auto;
  padding: 14px 6%;
  background:
    radial-gradient(rgba(134,150,160,.045) 1px, transparent 1.5px) 0 0/22px 22px,
    var(--thread-bg);
}
.load-more {
  display: block; margin: 0 auto 12px;
  background: var(--sidebar); color: var(--text-2);
  font-size: 12.5px; padding: 6px 14px; border-radius: 14px;
  border: 1px solid var(--border);
}
.load-more:hover { color: var(--text); }

.msgs { display: flex; flex-direction: column; gap: 3px; }
.day-sep {
  align-self: center; margin: 10px 0 6px;
  background: var(--sidebar); color: var(--text-2);
  font-size: 12px; padding: 4px 12px; border-radius: 8px;
  box-shadow: 0 1px 2px rgba(0,0,0,.3);
}

.msg {
  max-width: 65%; min-width: 90px;
  padding: 6px 9px 7px;
  border-radius: 8px;
  position: relative;
  box-shadow: 0 1px 1px rgba(0,0,0,.25);
  word-wrap: break-word;
}
.msg.in  { align-self: flex-start; background: var(--in);  border-top-left-radius: 2px; }
.msg.out { align-self: flex-end;   background: var(--out); border-top-right-radius: 2px; }
.msg-body { font-size: 14px; white-space: pre-wrap; }
.msg-caption { font-size: 13px; white-space: pre-wrap; margin-top: 4px; }
.msg-meta {
  display: flex; justify-content: flex-end; align-items: center; gap: 4px;
  margin-top: 3px; font-size: 11px; color: rgba(233,237,239,.6);
  user-select: none;
}
.tick { font-size: 12px; letter-spacing: -2px; color: rgba(233,237,239,.6); }
.tick.read { color: var(--read); }
.tick.failed { color: var(--danger); letter-spacing: 0; }

.msg img.media { max-width: 100%; max-height: 320px; border-radius: 6px; display: block; cursor: pointer; }
.msg video.media { max-width: 100%; max-height: 320px; border-radius: 6px; display: block; }
.msg audio.media { width: 260px; max-width: 100%; display: block; }
.msg .doc-link {
  display: flex; align-items: center; gap: 8px;
  background: rgba(0,0,0,.2); border-radius: 6px;
  padding: 10px 12px; text-decoration: none; color: var(--text);
  font-size: 13.5px;
}
.msg .doc-link:hover { background: rgba(0,0,0,.35); }
.msg-block {
  background: rgba(0,0,0,.2); border-left: 3px solid var(--accent);
  border-radius: 6px; padding: 8px 10px;
  font-size: 13px; display: flex; flex-direction: column; gap: 3px;
}
.msg-block .blk-title { font-weight: 600; color: var(--accent-hi); font-size: 12.5px; }
.msg pre.raw {
  background: rgba(0,0,0,.25); border-radius: 6px; padding: 8px;
  font-size: 11.5px; overflow-x: auto; max-width: 100%;
}

/* template rapido nel composer */
.tpl-quick {
  display: flex; gap: 8px; align-items: center;
  padding: 8px 14px; background: var(--sidebar);
  border-top: 1px solid var(--border);
}
.tpl-quick select { max-width: 260px; }

/* composer */
.composer {
  display: flex; align-items: flex-end; gap: 6px;
  padding: 8px 14px;
  background: var(--sidebar);
  border-top: 1px solid var(--border);
}
.composer textarea {
  flex: 1; resize: none; max-height: 140px;
  border-radius: 10px; padding: 9px 14px; font-size: 14px;
  line-height: 1.4;
}
.composer .send { color: var(--accent); font-size: 20px; }
.composer .send:hover { color: var(--accent-hi); background: var(--input); }

/* ---------- template ---------- */
.tpl-list { display: flex; flex-direction: column; gap: 12px; }
.tpl-card {
  background: var(--sidebar); border: 1px solid var(--border);
  border-radius: 10px; padding: 12px 14px;
  display: flex; flex-direction: column; gap: 6px;
}
.tpl-card-head { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.tpl-name { font-weight: 600; font-size: 14px; }
.pill {
  font-size: 11px; padding: 2px 8px; border-radius: 10px;
  background: var(--input); color: var(--text-2);
}
.pill.approved { background: #054d40; color: var(--accent-hi); }
.pill.rejected { background: #5b1a22; color: var(--danger); }
.pill.pending  { background: #4d3d05; color: var(--warn); }
.tpl-body { font-size: 13px; color: var(--text); white-space: pre-wrap; }
.tpl-del { margin-left: auto; }

/* ---------- profilo ---------- */
.kv-list { display: flex; flex-direction: column; gap: 6px; }
.kv-row { display: flex; gap: 10px; font-size: 13.5px; }
.kv-key { color: var(--text-2); min-width: 160px; flex-shrink: 0; }
.kv-val { word-break: break-word; }

/* ---------- console ---------- */
.console-row { display: flex; gap: 8px; }
.console-row select { width: 105px; flex-shrink: 0; }
.console-row input { flex: 1; font-family: ui-monospace, Consolas, monospace; font-size: 13px; }
.console-resp-head { display: flex; align-items: center; justify-content: space-between; }
.status-badge {
  font-size: 12.5px; font-weight: 700; padding: 2px 10px; border-radius: 10px;
  font-family: ui-monospace, Consolas, monospace;
}
.status-badge.ok   { background: #054d40; color: var(--accent-hi); }
.status-badge.warn { background: #4d3d05; color: var(--warn); }
.status-badge.err  { background: #5b1a22; color: var(--danger); }
.json-pre {
  background: var(--thread-bg); border: 1px solid var(--border);
  border-radius: 8px; padding: 12px;
  font-family: ui-monospace, Consolas, monospace; font-size: 12px;
  overflow: auto; max-height: 420px; white-space: pre-wrap; word-break: break-word;
}
.c-history { display: flex; flex-direction: column; gap: 6px; max-height: 560px; overflow-y: auto; }
.c-hist-item {
  display: flex; gap: 8px; align-items: center;
  background: var(--input); border-radius: 8px;
  padding: 7px 10px; font-size: 12.5px; text-align: left;
  font-family: ui-monospace, Consolas, monospace;
}
.c-hist-item:hover { filter: brightness(1.15); }
.c-hist-method { font-weight: 700; color: var(--accent-hi); width: 52px; flex-shrink: 0; }
.c-hist-path { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.c-hist-status { flex-shrink: 0; }
.c-hist-status.ok { color: var(--accent-hi); }
.c-hist-status.err { color: var(--danger); }

/* ---------- hooklog ---------- */
.hl-list { display: flex; flex-direction: column; gap: 8px; max-width: 980px; }
.hl-item {
  background: var(--sidebar); border: 1px solid var(--border);
  border-radius: 8px; overflow: hidden;
}
.hl-item summary {
  cursor: pointer; padding: 9px 14px; font-size: 13px;
  display: flex; gap: 12px; align-items: baseline;
  user-select: none; list-style: none;
}
.hl-item summary::-webkit-details-marker { display: none; }
.hl-item summary:hover { background: var(--input); }
.hl-id { color: var(--text-2); font-family: ui-monospace, Consolas, monospace; font-size: 12px; }
.hl-hint { color: var(--accent-hi); font-size: 12px; }
.hl-item pre { margin: 0 14px 12px; }

/* ---------- impostazioni ---------- */
.test-result { font-size: 13.5px; min-height: 18px; }
.test-result.ok { color: var(--accent-hi); }
.test-result.err { color: var(--danger); }
.wh-row {
  display: flex; align-items: center; gap: 10px;
  background: var(--thread-bg); border: 1px solid var(--border);
  border-radius: 8px; padding: 8px 12px;
}
.wh-row code { flex: 1; word-break: break-all; color: var(--accent-hi); }
.tok-create { display: flex; gap: 8px; }
.tok-create input { flex: 1; }
.tok-new {
  background: #054d40; border: 1px solid var(--accent);
  border-radius: 8px; padding: 10px 12px;
  display: flex; flex-direction: column; gap: 8px; font-size: 13px;
}
.tok-list { display: flex; flex-direction: column; gap: 6px; }
.tok-item {
  display: flex; align-items: center; gap: 10px;
  background: var(--input); border-radius: 8px; padding: 8px 12px;
  font-size: 13.5px;
}
.tok-item .tok-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tok-item .tok-date { color: var(--text-2); font-size: 12px; flex-shrink: 0; }

/* tag di attribuzione nella bolla (bot / app integrata) */
.msg-src {
  font-size: 10.5px; opacity: .75; letter-spacing: .2px;
  padding: 0 5px; border-radius: 6px; background: rgba(255,255,255,.09);
}

/* popup informativo */
.modal {
  position: fixed; inset: 0; z-index: 100;
  background: rgba(0,0,0,.6);
  display: flex; align-items: center; justify-content: center; padding: 20px;
}
.modal.hidden { display: none; }
.modal-box {
  background: var(--sidebar); border: 1px solid var(--border); border-radius: 12px;
  padding: 20px 22px; max-width: 520px; box-shadow: 0 18px 50px rgba(0,0,0,.5);
}
.modal-box h3 { margin-bottom: 10px; font-size: 15px; }
.modal-box p { font-size: 13.5px; color: var(--text-2); line-height: 1.55; margin-bottom: 16px; }

/* ---------- scrollbar ---------- */
::-webkit-scrollbar { width: 7px; height: 7px; }
::-webkit-scrollbar-thumb { background: rgba(134,150,160,.35); border-radius: 4px; }
::-webkit-scrollbar-track { background: transparent; }

/* ---------- responsive ---------- */
@media (max-width: 1100px) {
  .two-col { grid-template-columns: 1fr; }
}
@media (max-width: 900px) {
  .sidebar {
    position: fixed; top: 0; bottom: 0; left: 0;
    width: 85%; max-width: 380px;
    transform: translateX(-102%);
    transition: transform .18s ease-out;
    box-shadow: 4px 0 24px rgba(0,0,0,.5);
  }
  .sidebar.open { transform: translateX(0); }
  .sb-toggle { display: block; }
  .view-head { padding-left: 56px; }
  .thread-head { padding-left: 56px; }
  .chat-empty { padding: 0 24px; }
  .msg { max-width: 85%; }
}

/* pagina di accesso: il modulo al centro, stessi mattoni della UI (.card,
   .btn) così non nasce un secondo linguaggio visivo per una pagina sola. */
body.login-page {
  display: flex; align-items: center; justify-content: center;
  min-height: 100vh; padding: 20px;
}
.login-card { width: 100%; max-width: 320px; }
.login-card h3 { text-align: center; font-size: 17px; margin-bottom: 4px; }
.login-card .btn { margin-top: 4px; }
.login-msg { color: var(--danger); font-size: 13px; min-height: 18px; margin: 0; }
.login-card code {
  display: inline-block; margin-top: 6px; padding: 6px 8px;
  background: var(--thread-bg); border-radius: 6px;
  font-size: 11.5px; color: var(--text); word-break: break-all;
}
