/* Test UI del sistema RAG del Comune di Villasalto.
   Look-and-feel allineato a Bootstrap Italia (design system dei comuni). */

:root {
  /* Palette Villasalto (Bootstrap Italia tema verde) */
  --primary: #007A52;
  --primary-dark: #005A3C;
  --primary-light: #E0F2EB;
  --text: #17324D;
  --text-muted: #5C6F82;
  --bg: #F5F5F5;
  --white: #FFFFFF;
  --border: #E3E7EB;
  --border-strong: #C5D2DC;
  --error: #CC334D;
  --error-bg: #FBEAED;

  /* Tipografia */
  --font-sans: "Titillium Web", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --font-serif: "Lora", Georgia, "Times New Roman", serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: var(--font-serif);
  background: var(--bg);
  color: var(--text);
  line-height: 1.55;
  font-size: 16px;
}

body {
  display: flex;
  flex-direction: column;
}

/* ============================================================
   Header
   ============================================================ */

.site-header {
  flex: 0 0 auto;
  background: var(--white);
  border-bottom: 4px solid var(--primary);
  box-shadow: 0 1px 3px rgba(23, 50, 77, 0.06);
}

.header-inner {
  max-width: 860px;
  margin: 0 auto;
  padding: 1rem 1.5rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}

.header-brand {
  display: flex;
  align-items: center;
  gap: 0.9rem;
  font-family: var(--font-sans);
}

.header-emblem {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--primary);
  color: var(--white);
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--font-sans);
  font-weight: 700;
  font-size: 1.25rem;
  flex-shrink: 0;
}

.header-eyebrow {
  font-family: var(--font-sans);
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  line-height: 1.2;
}

.site-header h1 {
  margin: 0.15rem 0 0;
  font-family: var(--font-sans);
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--text);
  line-height: 1.2;
}

.header-controls {
  display: flex;
  align-items: center;
  gap: 1rem;
  font-family: var(--font-sans);
}

.debug-toggle {
  font-size: 0.85rem;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  gap: 0.35rem;
  cursor: pointer;
  user-select: none;
}

.debug-toggle input[type="checkbox"] {
  accent-color: var(--primary);
  cursor: pointer;
}

.btn-outline {
  font-family: var(--font-sans);
  font-size: 0.9rem;
  font-weight: 600;
  padding: 0.5rem 1rem;
  cursor: pointer;
  border-radius: 4px;
  border: 2px solid var(--primary);
  background: transparent;
  color: var(--primary);
  transition: background 0.15s, color 0.15s;
}

.btn-outline:hover {
  background: var(--primary);
  color: var(--white);
}

/* ============================================================
   Main / chat
   ============================================================ */

main {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 2rem 1rem;
}

.chat {
  max-width: 860px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.empty-hint {
  text-align: center;
  padding: 3.5rem 1.5rem;
  background: var(--white);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
}

.empty-title {
  margin: 0 0 0.35rem;
  font-family: var(--font-sans);
  font-size: 1.35rem;
  font-weight: 700;
  color: var(--primary);
}

.empty-sub {
  margin: 0;
  color: var(--text-muted);
  font-size: 0.95rem;
}

/* ============================================================
   Messaggi
   ============================================================ */

.msg {
  padding: 1rem 1.15rem;
  border-radius: 8px;
  max-width: 85%;
  border: 1px solid var(--border);
  background: var(--white);
}

.msg.user {
  align-self: flex-end;
  white-space: pre-wrap;
  background: var(--primary);
  color: var(--white);
  border-color: var(--primary);
  font-family: var(--font-sans);
  font-size: 0.98rem;
}

.msg.assistant {
  align-self: flex-start;
  box-shadow: 0 1px 3px rgba(23, 50, 77, 0.04);
}

.msg.error {
  align-self: stretch;
  max-width: 100%;
  background: var(--error-bg);
  border-color: var(--error);
  color: var(--error);
  font-family: var(--font-sans);
  font-size: 0.92rem;
}

/* Markdown dentro le risposte assistant */
.msg.assistant .md {
  font-family: var(--font-serif);
  font-size: 1rem;
  color: var(--text);
}
.msg.assistant .md > :first-child { margin-top: 0; }
.msg.assistant .md > :last-child { margin-bottom: 0; }
.msg.assistant .md p { margin: 0.6rem 0; }
.msg.assistant .md ul,
.msg.assistant .md ol {
  margin: 0.6rem 0;
  padding-left: 1.4rem;
}
.msg.assistant .md li { margin: 0.3rem 0; }
.msg.assistant .md strong {
  color: var(--text);
  font-weight: 700;
}
.msg.assistant .md h1,
.msg.assistant .md h2,
.msg.assistant .md h3 {
  margin: 1rem 0 0.4rem;
  font-family: var(--font-sans);
  font-weight: 700;
  color: var(--primary-dark);
}
.msg.assistant .md h1 { font-size: 1.2rem; }
.msg.assistant .md h2 { font-size: 1.1rem; }
.msg.assistant .md h3 { font-size: 1rem; }
.msg.assistant .md a {
  color: var(--primary);
  text-decoration: underline;
  text-underline-offset: 2px;
  word-break: break-word;
}
.msg.assistant .md a:hover {
  color: var(--primary-dark);
}
.msg.assistant .md code {
  background: var(--bg);
  padding: 0.1rem 0.35rem;
  border-radius: 3px;
  font-size: 0.9em;
  font-family: "SF Mono", Menlo, Consolas, monospace;
}
.msg.assistant .md pre {
  background: var(--bg);
  padding: 0.75rem 1rem;
  border-radius: 6px;
  overflow-x: auto;
  border: 1px solid var(--border);
}
.msg.assistant .md pre code {
  background: transparent;
  padding: 0;
}
.msg.assistant .md blockquote {
  margin: 0.6rem 0;
  padding: 0.3rem 0 0.3rem 1rem;
  border-left: 3px solid var(--primary);
  color: var(--text-muted);
  font-style: italic;
}

/* Cursore lampeggiante durante lo streaming */
.msg.assistant .cursor {
  display: inline-block;
  color: var(--primary);
  animation: blink 1s steps(2, start) infinite;
  margin-left: 2px;
  font-weight: 300;
}

@keyframes blink {
  to { visibility: hidden; }
}

/* ============================================================
   Blocco fonti
   ============================================================ */

.msg .sources {
  margin-top: 1rem;
  padding-top: 0.8rem;
  border-top: 1px solid var(--border);
  font-family: var(--font-sans);
  font-size: 0.88rem;
}

.msg .sources-section + .sources-section {
  margin-top: 1rem;
  padding-top: 0.8rem;
  border-top: 1px dashed var(--border);
}

.msg .sources-title {
  display: block;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 0.45rem;
  font-size: 0.82rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.msg .sources ol {
  list-style: none;
  margin: 0;
  padding: 0;
  counter-reset: src;
}

.msg .sources li {
  counter-increment: src;
  position: relative;
  padding: 0.4rem 0 0.4rem 2rem;
  border-bottom: 1px solid var(--border);
}

.msg .sources li:last-child {
  border-bottom: none;
}

.msg .sources li::before {
  content: counter(src);
  position: absolute;
  left: 0;
  top: 0.5rem;
  width: 1.5rem;
  height: 1.5rem;
  border-radius: 50%;
  background: var(--primary-light);
  color: var(--primary-dark);
  font-family: var(--font-sans);
  font-weight: 700;
  font-size: 0.78rem;
  display: flex;
  align-items: center;
  justify-content: center;
}

.msg .sources a {
  color: var(--primary);
  text-decoration: none;
  font-weight: 600;
}

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

.msg .sources .source-meta {
  display: block;
  margin-top: 0.15rem;
  font-size: 0.75rem;
  font-weight: 400;
  color: var(--text-muted);
  font-style: italic;
}

.msg .sources .score {
  color: var(--text-muted);
  font-size: 0.78rem;
  margin-left: 0.4rem;
  font-weight: 400;
}

/* ============================================================
   Form di input
   ============================================================ */

.ask-form {
  flex: 0 0 auto;
  background: var(--white);
  border-top: 1px solid var(--border);
  padding: 1rem 1rem 0.75rem;
}

.ask-form-inner {
  max-width: 860px;
  margin: 0 auto;
  display: flex;
  gap: 0.6rem;
  align-items: flex-end;
}

.ask-form textarea {
  flex: 1 1 auto;
  padding: 0.7rem 1rem;
  font-family: var(--font-serif);
  font-size: 1rem;
  color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  resize: none;
  max-height: 180px;
  overflow-y: auto;
  line-height: 1.5;
  background: var(--white);
  transition: border-color 0.15s, box-shadow 0.15s;
}

.ask-form textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(0, 122, 82, 0.2);
}

.ask-form textarea::placeholder {
  color: var(--text-muted);
  font-style: italic;
}

.ask-form button {
  flex: 0 0 auto;
  padding: 0 1.5rem;
  height: 46px;
  font-family: var(--font-sans);
  font-size: 0.95rem;
  font-weight: 700;
  cursor: pointer;
  border-radius: 6px;
  border: 2px solid var(--primary);
  background: var(--primary);
  color: var(--white);
  transition: background 0.15s, border-color 0.15s;
}

.ask-form button:hover:not(:disabled) {
  background: var(--primary-dark);
  border-color: var(--primary-dark);
}

.ask-form button:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.ask-form-footer {
  max-width: 860px;
  margin: 0.6rem auto 0;
  text-align: center;
  font-family: var(--font-sans);
  font-size: 0.75rem;
  color: var(--text-muted);
}

/* ============================================================
   Pannello debug
   ============================================================ */

.debug {
  max-width: 860px;
  margin: 1.5rem auto 0;
  padding: 1rem 1.2rem;
  background: #17324D;
  color: #E3E7EB;
  border-radius: 8px;
  font-family: var(--font-sans);
  font-size: 0.85rem;
}

.debug h2 {
  margin: 0 0 0.5rem;
  font-size: 0.85rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: #8CB4FF;
}

.debug pre {
  margin: 0;
  white-space: pre-wrap;
  word-break: break-word;
  font-family: "SF Mono", Menlo, Consolas, monospace;
  font-size: 0.78rem;
  line-height: 1.5;
}

.hidden {
  display: none;
}

/* ============================================================
   Mobile
   ============================================================ */

@media (max-width: 640px) {
  .header-inner { padding: 0.75rem 1rem; }
  .header-eyebrow { font-size: 0.7rem; }
  .site-header h1 { font-size: 1.05rem; }
  .btn-outline { padding: 0.4rem 0.75rem; font-size: 0.82rem; }
  main { padding: 1.25rem 0.75rem; }
  .msg { max-width: 92%; padding: 0.85rem 1rem; }
  .empty-hint { padding: 2.25rem 1rem; }
  .empty-title { font-size: 1.15rem; }
}
