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

:root {
  --primary:       #1a2b4a;
  --primary-hover: #243d6a;
  --header-bg:     var(--primary);
  --chat-bg:       #f3f6fc;
  --chat-bg-image: url("https://unindustriabeta.tim-lab.it/un-industria.it/Prj/ImgToo/Banner_Unibiz.jpg");
  /* Velo chiaro sopra l'immagine per mantenere leggibili i fumetti */
  --chat-bg-scrim: rgba(243, 246, 252, .62);
  --font-family:   system-ui, sans-serif;
}

body {
  font-family: var(--font-family);
}

/* ── Pulsante floating (chat chiusa) ─────────────────────────────────────── */

#chat-toggle {
  position: fixed;
  bottom: 24px;
  right: 24px;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 16px rgba(0,0,0,.25);
  transition: background .2s, transform .15s;
  z-index: 999;
}

#chat-toggle:hover { background: var(--primary-hover); transform: scale(1.07); }
#chat-toggle svg   { width: 24px; height: 24px; }
#chat-toggle img   { width: 28px; height: 28px; object-fit: contain; }
#chat-toggle.hidden { display: none; }

/* ── Pannello chat ───────────────────────────────────────────────────────── */

#app {
  position: fixed;
  bottom: 24px;
  right: 24px;
  width: 380px;
  height: 560px;
  display: flex;
  flex-direction: column;
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 8px 40px rgba(0,0,0,.18);
  overflow: hidden;
  z-index: 1000;
  transition: width .25s ease, height .25s ease,
              top .25s ease, bottom .25s ease,
              right .25s ease, left .25s ease,
              border-radius .25s ease;
}

#app.hidden { display: none; }

#app.expanded {
  top: 16px;
  bottom: 16px;
  right: 16px;
  left: 16px;
  width: auto;
  height: auto;
  border-radius: 12px;
}

/* ── Header ──────────────────────────────────────────────────────────────── */

header {
  padding: 18px 16px;
  background: var(--header-bg);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
  position: relative;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 8px;
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
}
.logo        { font-size: 1.05rem; font-weight: 700; letter-spacing: .05em; }
.logo img    { height: 40px; width: auto; max-width: 220px; display: block; }
.subtitle    { font-size: .78rem; opacity: .65; align-self: flex-end; margin-bottom: 1px; }

.header-actions { display: flex; gap: 4px; }

.header-actions button {
  background: none;
  border: none;
  color: #fff;
  opacity: .7;
  cursor: pointer;
  padding: 4px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: opacity .15s, background .15s;
}

.header-actions button:hover { opacity: 1; background: rgba(255,255,255,.12); }
.header-actions button svg   { width: 16px; height: 16px; }

/* ── Messaggi ────────────────────────────────────────────────────────────── */

#messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-height: 0;
  background:
    linear-gradient(var(--chat-bg-scrim), var(--chat-bg-scrim)),
    var(--chat-bg-image) center / cover no-repeat;
  background-color: var(--chat-bg);
}

.bubble {
  max-width: 85%;
  padding: 9px 13px;
  border-radius: 12px;
  line-height: 1.5;
  font-size: .9rem;
  word-break: break-word;
}

.bubble.user {
  align-self: flex-end;
  background: var(--primary);
  color: #fff;
  border-bottom-right-radius: 4px;
  white-space: pre-wrap;
}

.bubble.assistant {
  align-self: flex-start;
  background: rgba(255, 255, 255, .94);
  color: #1a1a1a;
  border-bottom-left-radius: 4px;
  backdrop-filter: blur(2px);
  box-shadow: 0 1px 4px rgba(26, 43, 74, .12);
}

.bubble.error {
  align-self: flex-start;
  background: #fdecea;
  color: #b71c1c;
  border-bottom-left-radius: 4px;
}

/* ── Markdown nel bubble assistant ──────────────────────────────────────── */

.bubble.assistant p             { margin: .4em 0; }
.bubble.assistant p:first-child { margin-top: 0; }
.bubble.assistant p:last-child  { margin-bottom: 0; }

.bubble.assistant h3,
.bubble.assistant h4,
.bubble.assistant h5,
.bubble.assistant h6 {
  margin: .7em 0 .25em;
  font-size: .95rem;
  font-weight: 700;
  color: var(--primary);
}

.bubble.assistant strong { font-weight: 700; }
.bubble.assistant em     { font-style: italic; }

.bubble.assistant ul,
.bubble.assistant ol {
  margin: .4em 0 .4em 1.2em;
  padding: 0;
}
.bubble.assistant li { margin: .2em 0; }

.bubble.assistant code {
  background: #dde2f0;
  border-radius: 4px;
  padding: 1px 5px;
  font-family: 'Consolas', 'Courier New', monospace;
  font-size: .83em;
}

.bubble.assistant pre {
  background: #1e2a3a;
  color: #e8edf5;
  border-radius: 8px;
  padding: 10px 12px;
  overflow-x: auto;
  margin: .6em 0;
}

.bubble.assistant pre code {
  background: none;
  padding: 0;
  font-size: .82em;
  color: inherit;
}

.bubble.assistant table {
  border-collapse: collapse;
  width: 100%;
  margin: .6em 0;
  font-size: .85rem;
}

.bubble.assistant th,
.bubble.assistant td {
  border: 1px solid #c5ccd8;
  padding: 4px 8px;
  text-align: left;
}

.bubble.assistant th                   { background: #d4d9e8; font-weight: 700; }
.bubble.assistant tr:nth-child(even) td { background: #f3f5fb; }

.bubble.assistant hr {
  border: none;
  border-top: 1px solid #c5ccd8;
  margin: .6em 0;
}

/* ── Fonti ───────────────────────────────────────────────────────────────── */

.sources {
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid rgba(0,0,0,.08);
  font-size: .76rem;
  color: #666;
  line-height: 1.6;
}

.sources-label {
  display: block;
  font-weight: 600;
  color: #444;
  margin-bottom: 4px;
}

.sources a, .sources span {
  color: var(--primary);
  text-decoration: none;
}

.sources a:hover { text-decoration: underline; }

/* ── Indicatore di digitazione ───────────────────────────────────────────── */

@keyframes typing-bounce {
  0%, 60%, 100% { transform: translateY(0);    opacity: .35; }
  30%            { transform: translateY(-6px); opacity: 1;   }
}

.typing {
  align-self: flex-start;
  background: rgba(255, 255, 255, .94);
  border-radius: 12px;
  border-bottom-left-radius: 4px;
  padding: 10px 14px;
  display: flex;
  align-items: center;
  gap: 5px;
  backdrop-filter: blur(2px);
  box-shadow: 0 1px 4px rgba(26, 43, 74, .12);
}

.typing span {
  display: inline-block;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--primary);
  animation: typing-bounce 1.1s ease-in-out infinite;
}

.typing span:nth-child(2) { animation-delay: .18s; }
.typing span:nth-child(3) { animation-delay: .36s; }

/* ── Form input ──────────────────────────────────────────────────────────── */

#chat-form {
  display: flex;
  gap: 8px;
  padding: 12px 14px;
  border-top: 1px solid #e0e0e0;
  background: #fff;
  flex-shrink: 0;
}

#input {
  flex: 1;
  padding: 9px 13px;
  border: 1px solid #ccc;
  border-radius: 8px;
  font-size: .92rem;
  outline: none;
  transition: border-color .2s;
  min-width: 0;
}

#input:focus { border-color: var(--primary); }

#send-btn {
  width: 38px;
  height: 38px;
  flex-shrink: 0;
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background .2s;
}

#send-btn svg      { width: 17px; height: 17px; }
#send-btn:hover    { background: var(--primary-hover); }
#send-btn:disabled { background: #aaa; cursor: not-allowed; }
