:root {
  /* Tema Clownfiesta — palette IDENTICA al portale .28 (index.php) */
  --bg: #050403;
  --bg-elev: #130d0c;            /* ~ --panel-strong */
  --bg-input: rgba(0,0,0,0.52);
  --gold: #d7a84f;               /* grassetto / azione primaria */
  --cream: #b5a28f;              /* richiami / muted / link */
  --red: #ef2020;               /* titoli / brand */
  --green: #27e36f;
  --accent: #d7a84f;            /* azione primaria (oro) */
  --accent-2: #b5a28f;
  --on-accent: #130d08;         /* testo scuro sopra l'oro */
  --text: #f4eadc;
  --text-dim: #b5a28f;
  --user: #1a1310;
  --bot: #0d0a08;
  --border: rgba(204,168,108,0.28);
  --radius: 16px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

/* l'attributo hidden DEVE vincere su display:flex delle classi (login/app) */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

body {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  background: radial-gradient(circle at top, #21130e 0, #090605 42%, #000 100%);
  background-attachment: fixed;
}

/* Topbar */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  padding-top: calc(12px + env(safe-area-inset-top, 0px));
  background: var(--bg-elev);
  border-bottom: 1px solid var(--border);
  flex: 0 0 auto;
}
.brand { font-weight: 800; font-size: 1.05rem; letter-spacing: .2px; color: var(--gold); margin-right: auto; }
.icon-btn {
  background: none; border: none; color: var(--text);
  font-size: 1.3rem; cursor: pointer; padding: 4px 8px; border-radius: 10px;
}
.icon-btn:active { background: var(--bg-input); }

/* Chat area */
.chat {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  scroll-behavior: smooth;
}

.empty { margin: auto; text-align: center; color: var(--text-dim); max-width: 320px; }
.empty-title { font-size: 1.15rem; color: var(--text); margin: 0 0 6px; }
.empty-sub { font-size: .92rem; line-height: 1.5; margin: 0; }

.msg {
  max-width: 85%;
  padding: 11px 14px;
  border-radius: var(--radius);
  line-height: 1.5;
  white-space: pre-wrap;
  word-wrap: break-word;
  font-size: .97rem;
  animation: pop .15s ease;
}
@keyframes pop { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; } }

.msg.user {
  align-self: flex-end;
  background: var(--user);
  border-bottom-right-radius: 4px;
}
.msg.bot {
  align-self: flex-start;
  background: var(--bot);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}
.msg.bot.thinking { color: var(--text-dim); font-style: italic; }
.msg .cursor { display: inline-block; width: 7px; background: var(--gold); animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: 0; } }

/* Composer */
.composer {
  flex: 0 0 auto;
  display: flex;
  gap: 8px;
  padding: 10px 12px;
  padding-bottom: calc(10px + var(--safe-b));
  background: var(--bg-elev);
  border-top: 1px solid var(--border);
}
#input {
  flex: 1;
  resize: none;
  max-height: 140px;
  background: var(--bg-input);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 11px 14px;
  font: inherit;
  font-size: 1rem;
  line-height: 1.4;
  outline: none;
}
#input:focus { border-color: var(--accent); }
.send-btn {
  flex: 0 0 auto;
  width: 46px; height: 46px;
  border: none; border-radius: 50%;
  background: var(--accent);
  color: var(--on-accent); font-size: 1.2rem; font-weight: 900;
  cursor: pointer;
  align-self: flex-end;
  transition: transform .1s, background .2s;
}
.send-btn:active { transform: scale(.92); }

.mode-btn {
  flex: 0 0 auto;
  width: 46px; height: 46px;
  border: 1px solid var(--border); border-radius: 50%;
  background: var(--bg-input); color: var(--text);
  font-size: 1.15rem; cursor: pointer; align-self: flex-end;
  transition: transform .1s, background .2s, border-color .2s;
}
.mode-btn:active { transform: scale(.92); }
.mode-btn.active { background: var(--gold); border-color: var(--gold); box-shadow: 0 0 0 3px rgba(215,168,79,0.22); }
.send-btn:disabled { background: var(--border); color: var(--text-dim); cursor: default; }

/* Settings dialog */
.settings {
  border: none;
  border-radius: 18px;
  background: var(--bg-elev);
  color: var(--text);
  padding: 0;
  width: min(440px, 92vw);
}
.settings::backdrop { background: rgba(0,0,0,.55); backdrop-filter: blur(2px); }
.settings form { padding: 22px; display: flex; flex-direction: column; gap: 14px; }
.settings h2 { margin: 0 0 4px; font-size: 1.2rem; }
.settings label { display: flex; flex-direction: column; gap: 6px; font-size: .85rem; color: var(--text-dim); }
.settings label.row { flex-direction: row; align-items: center; gap: 8px; color: var(--text); }
.settings input[type="url"],
.settings input[type="password"],
.settings input[type="text"] {
  background: var(--bg-input); color: var(--text);
  border: 1px solid var(--border); border-radius: 10px;
  padding: 10px 12px; font: inherit; outline: none;
}
.settings input:focus { border-color: var(--accent); }
.settings-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 4px; }
.btn-primary, .btn-ghost {
  border: none; border-radius: 10px; padding: 10px 18px; font: inherit; font-weight: 600; cursor: pointer;
}
.btn-primary { background: var(--accent); color: white; }
.btn-ghost { background: transparent; color: var(--text-dim); }
.hint { font-size: .78rem; color: var(--text-dim); margin: 2px 0 0; line-height: 1.4; }

/* Guida */
.help-body { padding: 22px; display: flex; flex-direction: column; gap: 12px; }
.help-body h2 { margin: 0; color: var(--gold); font-size: 1.2rem; }
.help-list { margin: 0; padding-left: 18px; display: flex; flex-direction: column; gap: 8px; font-size: .92rem; line-height: 1.45; }
.help-list b { color: var(--gold); }
.help-body .btn-primary { background: var(--accent); color: var(--on-accent); border: none; border-radius: 10px; padding: 11px; font-weight: 800; cursor: pointer; }
.help-body .btn-ghost { background: transparent; color: var(--text-dim); border: none; padding: 6px; cursor: pointer; }

/* Login */
.login-screen {
  position: fixed; inset: 0;
  display: flex; align-items: center; justify-content: center;
  padding: 24px;
  background:
    radial-gradient(1200px 600px at 50% -10%, var(--bg-elev), var(--bg));
}
.login-card {
  width: min(380px, 100%);
  display: flex; flex-direction: column; gap: 12px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 30px 26px;
  box-shadow: 0 20px 60px rgba(0,0,0,.4);
}
.login-img {
  display: block; width: 100%; max-height: 320px; object-fit: cover;
  border-radius: 14px; border: 1px solid var(--border);
  margin-bottom: 6px;
}
.login-title { margin: 0; text-align: center; font-size: 1.5rem; color: var(--gold); font-weight: 800; }
.login-sub { margin: 0 0 8px; text-align: center; color: var(--text-dim); font-size: .9rem; }
.login-card input {
  background: var(--bg-input); color: var(--text);
  border: 1px solid var(--border); border-radius: 12px;
  padding: 13px 14px; font: inherit; font-size: 1rem; outline: none;
}
.login-card input:focus { border-color: var(--accent); }
.login-submit {
  margin-top: 4px;
  background: var(--accent); color: var(--on-accent);
  border: none; border-radius: 12px; padding: 13px;
  font: inherit; font-weight: 850; font-size: 1rem; cursor: pointer;
  transition: transform .1s, filter .2s;
}
.login-submit:active { transform: scale(.98); }
.login-submit:disabled { filter: saturate(.5) brightness(.8); cursor: default; }
.login-err { margin: 2px 0 0; color: #ff9db0; font-size: .85rem; text-align: center; }

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

/* Pannello laterale chat */
.drawer-bg { position: fixed; inset: 0; background: rgba(0,0,0,.55); z-index: 20; }
.drawer {
  position: fixed; top: 0; left: 0; bottom: 0; z-index: 21;
  width: min(300px, 82vw);
  background: var(--bg-elev); border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  padding: calc(12px + env(safe-area-inset-top,0px)) 12px 12px;
  box-shadow: 8px 0 30px rgba(0,0,0,.5);
}
.drawer-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.drawer-head span { font-weight: 800; color: var(--gold); }
.btn-primary.sm { padding: 7px 12px; font-size: .85rem; border-radius: 9px; background: var(--accent); color: var(--on-accent); border: none; font-weight: 800; cursor: pointer; }
.chat-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; display: flex; flex-direction: column; gap: 4px; }
.chat-item {
  display: flex; align-items: center; gap: 6px;
  padding: 10px 10px; border-radius: 10px; cursor: pointer;
  border: 1px solid transparent;
}
.chat-item:hover { background: var(--bg-input); }
.chat-item.active { background: var(--bg-input); border-color: var(--border); }
.chat-item .t { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: .92rem; }
.chat-item .del { background: none; border: none; color: var(--text-dim); cursor: pointer; font-size: 1rem; padding: 2px 4px; border-radius: 6px; }
.chat-item .del:hover { color: var(--red); background: rgba(239,32,32,.12); }
.chat-empty { color: var(--text-dim); font-size: .85rem; text-align: center; padding: 16px 8px; }

/* Markdown dentro le bolle del bot */
.msg.bot p { margin: 0 0 8px; }
.msg.bot p:last-child { margin-bottom: 0; }
.msg.bot h3, .msg.bot h4, .msg.bot h5, .msg.bot h6 { margin: 10px 0 6px; line-height: 1.25; color: var(--red); }
.msg.bot h3 { font-size: 1.08rem; } .msg.bot h4 { font-size: 1rem; }
.msg.bot ul, .msg.bot ol { margin: 4px 0 8px; padding-left: 22px; }
.msg.bot li { margin: 3px 0; }
.msg.bot strong { color: var(--gold); font-weight: 700; }
.msg.bot em { color: var(--cream); font-style: normal; }
.msg.bot a { color: var(--cream); text-decoration: underline; }
.msg.bot code {
  background: var(--bg-input); color: var(--accent-2);
  padding: 1px 6px; border-radius: 6px; font-size: .9em;
  font-family: ui-monospace, "Cascadia Code", Consolas, monospace;
}
.msg.bot pre {
  background: var(--bg); border: 1px solid var(--border);
  border-radius: 10px; padding: 12px; overflow-x: auto; margin: 8px 0;
}
.msg.bot pre code { background: none; color: var(--text); padding: 0; }
.msg.bot .gen-img {
  display: block; width: 100%; max-width: 420px;
  margin: 10px 0 2px; border-radius: 12px; border: 1px solid var(--border);
}
.msg.bot .cite {
  color: var(--gold); font-weight: 700; font-size: .7em;
  background: var(--bg-input); padding: 1px 5px; border-radius: 6px; margin-left: 2px;
}

/* Error toast */
.toast {
  align-self: center;
  background: #4a2330; color: #ffd9e0;
  border: 1px solid #7a3548;
  padding: 8px 14px; border-radius: 12px;
  font-size: .85rem; max-width: 90%;
}
