/* Chateraphy — interfaccia della chat. Nessuna dipendenza esterna. */
:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --surface-2: #efeee8;
  --text: #1c1f1e;
  --muted: #646b68;
  --line: #dddbd3;
  --accent: #2f6b63;
  --accent-ink: #ffffff;
  --user: #2f6b63;
  --user-ink: #ffffff;
  --danger: #a3372c;
  --notice-bg: #fff6e8;
  --notice-line: #e8c58f;
  --radius: 18px;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 8px 24px rgba(0,0,0,.06);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111413;
    --surface: #1a1e1d;
    --surface-2: #232827;
    --text: #e8ebe9;
    --muted: #9aa39f;
    --line: #2e3533;
    --accent: #6fbfb2;
    --accent-ink: #0d1615;
    --user: #2b5f58;
    --user-ink: #eef6f4;
    --danger: #ff8a7a;
    --notice-bg: #2a2418;
    --notice-line: #6b5530;
    --shadow: 0 1px 2px rgba(0,0,0,.4);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.55 var(--font);
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
}
button, textarea, input { font: inherit; color: inherit; }
a { color: var(--accent); }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  max-width: 820px;
  margin: 0 auto;
}

/* Barra superiore */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: max(10px, env(safe-area-inset-top)) 16px 10px;
}
.brand {
  font-weight: 700;
  letter-spacing: .18em;
  font-size: 13px;
  text-decoration: none;
  color: var(--text);
}
.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 12px;
  background: transparent;
  cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); }
.icon-btn svg { width: 22px; height: 22px; }
.topbar-actions { display: flex; gap: 4px; }

/* Area messaggi */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 8px 16px 24px;
  scroll-behavior: smooth;
  overscroll-behavior: contain;
}
.empty {
  min-height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 24px 8px 48px;
}
.empty h1 {
  margin: 0 0 4px;
  font-size: clamp(14px, 3.4vw, 15px);
  letter-spacing: .32em;
  font-weight: 700;
  color: var(--muted);
}
.empty p.welcome {
  margin: 0;
  font-size: clamp(40px, 11vw, 64px);
  font-weight: 600;
  letter-spacing: -.02em;
  line-height: 1.1;
}
.empty .hint { margin-top: 16px; color: var(--muted); font-size: 14px; max-width: 34em; }

.msg { display: flex; margin: 14px 0; }
.msg.user { justify-content: flex-end; }
.bubble {
  max-width: min(85%, 640px);
  padding: 11px 15px;
  border-radius: var(--radius);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.msg.user .bubble { background: var(--user); color: var(--user-ink); border-bottom-right-radius: 6px; }
.msg.assistant .bubble { background: var(--surface); box-shadow: var(--shadow); border-bottom-left-radius: 6px; }
.msg.assistant.interrupted .bubble::after { content: " (interrotta)"; color: var(--muted); font-size: 13px; }
.msg.error .bubble { background: transparent; border: 1px solid var(--danger); color: var(--danger); font-size: 14px; }

.typing { display: inline-flex; gap: 4px; padding: 4px 0; }
.typing i { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); opacity: .4; animation: blink 1.2s infinite; }
.typing i:nth-child(2) { animation-delay: .2s; }
.typing i:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }
@media (prefers-reduced-motion: reduce) { .typing i { animation: none; opacity: .7; } .messages { scroll-behavior: auto; } }

/* Riquadro risorse di emergenza */
.notice {
  margin: 10px 0 16px;
  padding: 14px 16px;
  border: 1px solid var(--notice-line);
  background: var(--notice-bg);
  border-radius: 14px;
}
.notice h2 { margin: 0 0 8px; font-size: 15px; }
.notice ul { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.notice li strong { display: block; }
.notice a.call {
  display: inline-block;
  margin-top: 2px;
  padding: 6px 12px;
  border-radius: 999px;
  background: var(--danger);
  color: #fff;
  text-decoration: none;
  font-weight: 600;
}
.notice small { color: var(--muted); display: block; }

/* Composer */
.composer {
  padding: 8px 12px max(12px, env(safe-area-inset-bottom));
  background: linear-gradient(to top, var(--bg) 70%, transparent);
}
.consent {
  margin: 0 4px 10px;
  padding: 12px 14px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 14px;
  font-size: 13px;
  color: var(--muted);
}
.consent label { display: flex; gap: 10px; align-items: flex-start; cursor: pointer; }
.consent input { margin-top: 3px; width: 18px; height: 18px; accent-color: var(--accent); flex: none; }
.input-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 6px 6px 6px 16px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 26px;
  box-shadow: var(--shadow);
}
.input-row:focus-within { border-color: var(--accent); }
textarea {
  flex: 1;
  border: 0;
  outline: 0;
  resize: none;
  background: transparent;
  padding: 9px 0;
  max-height: 40vh;
  min-height: 24px;
  line-height: 1.45;
}
.send {
  flex: none;
  width: 42px;
  height: 42px;
  border-radius: 50%;
  border: 0;
  background: var(--accent);
  color: var(--accent-ink);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.send:disabled { opacity: .4; cursor: default; }
.send svg { width: 20px; height: 20px; }
.footnote { text-align: center; color: var(--muted); font-size: 12px; margin: 8px 8px 0; }
.counter { text-align: right; font-size: 12px; color: var(--muted); margin: 4px 14px 0; min-height: 1em; }
.counter.over { color: var(--danger); }

/* Pannello laterale */
.drawer-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.35); opacity: 0; pointer-events: none; transition: opacity .2s; z-index: 10; }
.drawer {
  position: fixed;
  top: 0; right: 0; bottom: 0;
  width: min(360px, 92vw);
  background: var(--surface);
  transform: translateX(100%);
  transition: transform .22s ease;
  z-index: 11;
  display: flex;
  flex-direction: column;
  padding: max(14px, env(safe-area-inset-top)) 0 max(14px, env(safe-area-inset-bottom));
  box-shadow: -8px 0 32px rgba(0,0,0,.15);
}
body.drawer-open .drawer { transform: none; }
body.drawer-open .drawer-backdrop { opacity: 1; pointer-events: auto; }
.drawer header { display: flex; justify-content: space-between; align-items: center; padding: 0 12px 8px 20px; }
.drawer h2 { font-size: 13px; letter-spacing: .14em; text-transform: uppercase; color: var(--muted); margin: 18px 20px 6px; font-weight: 600; }
.drawer-body { overflow-y: auto; flex: 1; }
.menu-btn {
  display: flex;
  width: 100%;
  align-items: center;
  gap: 12px;
  padding: 11px 20px;
  border: 0;
  background: none;
  text-align: left;
  cursor: pointer;
  text-decoration: none;
  color: var(--text);
}
.menu-btn:hover { background: var(--surface-2); }
.menu-btn.danger { color: var(--danger); }
.menu-btn small { color: var(--muted); display: block; }
.conv-list { list-style: none; margin: 0; padding: 0; }
.conv-list .menu-btn { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: block; }
.conv-list .active { background: var(--surface-2); font-weight: 600; }
.switch { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 20px; }
.switch input { width: 20px; height: 20px; accent-color: var(--accent); }
.memory-items { list-style: none; margin: 0; padding: 0 20px; font-size: 14px; color: var(--muted); }
.memory-items li { display: flex; justify-content: space-between; gap: 8px; padding: 4px 0; }
.memory-items button { border: 0; background: none; color: var(--danger); cursor: pointer; font-size: 13px; }

.toast {
  position: fixed;
  left: 50%;
  bottom: 110px;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 9px 16px;
  border-radius: 999px;
  font-size: 14px;
  z-index: 20;
  opacity: 0;
  transition: opacity .2s;
  pointer-events: none;
}
.toast.show { opacity: 1; }

@media (min-width: 720px) {
  .messages { padding: 8px 24px 32px; }
  .composer { padding: 8px 24px 20px; }
}
