:root {
  --primary: #1B3C6E;
  --primary-dark: #12294C;
  --primary-hover: #264E86;
  --accent: #C41E3A;
  --accent-hover: #A51730;
  --brand-gray: #7A8C9A;

  --bg: #F5F7FA;
  --panel: #FFFFFF;
  --panel-3: #EDF1F7;
  --panel-hover: #E4EAF2;
  --text: #0F1B2D;
  --muted: #64748B;
  --border: #E1E6EE;
  --border-strong: #CBD3DE;
  --code-bg: #EDF1F7;
}

* { box-sizing: border-box; }
html { height: 100%; }
body {
  margin: 0;
  height: 100dvh;      /* viewport dinamico — evita bug address bar mobile */
  min-height: 100vh;   /* fallback per browser senza dvh */
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
  overflow: hidden;    /* niente scroll body, scrolliamo nel .messages */
}

.wrap {
  display: grid;
  grid-template-columns: 1.2fr 1fr;
  height: 100%;
}

/* ============ AVATAR PANEL ============ */
.avatar {
  background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
  color: white;
  min-height: 0;
}
.avatar::before {
  content: "";
  position: absolute;
  inset: 0;
  background: radial-gradient(800px 400px at 30% 20%, rgba(255,255,255,0.08) 0%, transparent 60%);
  pointer-events: none;
}
/* Stage che contiene placeholder, video e loader sovrapposti */
.video-stage {
  position: relative;
  width: min(80%, 480px);
  aspect-ratio: 3/4;
  border-radius: 24px;
  overflow: hidden;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid rgba(255, 255, 255, 0.1);
  box-shadow: 0 30px 80px rgba(0, 0, 0, 0.3);
  z-index: 1;
}
.video-placeholder {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: rgba(255, 255, 255, 0.6);
  transition: opacity 0.3s;
}
.placeholder-label {
  color: rgba(255, 255, 255, 0.6);
  margin-top: 12px;
  font-size: 14px;
}
#avatarVideo {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  background: #0F1B2D;
  opacity: 0;
  transition: opacity 0.4s ease-out;
}
#avatarAudio {
  display: none;
}
.video-stage.live #avatarVideo { opacity: 1; }
.video-stage.live .video-placeholder { opacity: 0; pointer-events: none; }

.video-loading {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: rgba(15, 27, 45, 0.85);
  backdrop-filter: blur(4px);
  z-index: 2;
  color: white;
  gap: 12px;
}
.video-loading[hidden] { display: none; }

/* ============ WAVEFORM (mic volume visualizer) ============ */
.waveform {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 3px;
  margin-top: 20px;
  height: 48px;
  width: min(80%, 480px);
  padding: 0 12px;
  animation: wf-fade 0.25s ease-out;
}
.waveform[hidden] { display: none; }
.waveform .bar {
  flex: 1;
  min-width: 3px;
  max-width: 6px;
  height: 20%;
  background: linear-gradient(180deg, rgba(255,255,255,0.95) 0%, rgba(42, 183, 234, 0.9) 100%);
  border-radius: 3px;
  transform-origin: center;
  transition: height 0.08s cubic-bezier(0.4, 0, 0.2, 1);
  box-shadow: 0 0 8px rgba(42, 183, 234, 0.35);
}
@keyframes wf-fade {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

/* Controlli media (mic, cam, end) — overlay fondo del video */
.media-controls {
  position: absolute;
  bottom: 12px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 8px;
  z-index: 3;
  padding: 6px;
  background: rgba(15, 27, 45, 0.55);
  backdrop-filter: blur(8px);
  border-radius: 999px;
  animation: fade-up 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}
.media-controls[hidden] { display: none; }
.ctrl-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 0;
  background: rgba(255, 255, 255, 0.12);
  color: white;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, transform 0.18s cubic-bezier(0.34, 1.56, 0.64, 1);
  -webkit-tap-highlight-color: transparent;
}
.ctrl-btn:hover { background: rgba(255, 255, 255, 0.22); transform: scale(1.06); }
.ctrl-btn:active { transform: scale(0.94); transition-duration: 0.08s; }
.ctrl-btn .ic-off { display: block; }
.ctrl-btn .ic-on { display: none; }
.ctrl-btn[data-on="true"] {
  background: white;
  color: var(--primary);
}
.ctrl-btn[data-on="true"] .ic-on { display: block; }
.ctrl-btn[data-on="true"] .ic-off { display: none; }
.ctrl-btn.ctrl-end { background: var(--accent); }
.ctrl-btn.ctrl-end:hover { background: var(--accent-hover); }
@keyframes fade-up {
  from { opacity: 0; transform: translate(-50%, 8px); }
  to { opacity: 1; transform: translate(-50%, 0); }
}
.loader-spinner {
  width: 36px;
  height: 36px;
  border: 3px solid rgba(255, 255, 255, 0.18);
  border-top-color: white;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
.loader-text { font-size: 13px; opacity: 0.85; }
.start-btn {
  position: absolute;
  bottom: 32px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--accent);
  color: white;
  border: 0;
  padding: 13px 26px;
  font-size: 14px;
  font-weight: 600;
  border-radius: 999px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  transition:
    background 0.2s cubic-bezier(0.4, 0, 0.2, 1),
    transform 0.25s cubic-bezier(0.34, 1.56, 0.64, 1),
    box-shadow 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  box-shadow: 0 6px 20px rgba(196, 30, 58, 0.3), 0 0 0 0 rgba(196, 30, 58, 0.4);
  z-index: 2;
  -webkit-tap-highlight-color: transparent;
  animation: pulse-attention 2.4s ease-in-out infinite;
}
.start-btn:hover {
  background: var(--accent-hover);
  transform: translateX(-50%) translateY(-2px) scale(1.04);
  box-shadow: 0 12px 30px rgba(196, 30, 58, 0.45), 0 0 0 0 rgba(196, 30, 58, 0.4);
  animation: none;
}
.start-btn:active { transform: translateX(-50%) translateY(0) scale(0.98); transition-duration: 0.08s; }
.start-btn:disabled { opacity: 0.6; cursor: default; transform: translateX(-50%); animation: none; }
.start-btn svg { transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1); }
.start-btn:hover svg { transform: translateX(2px); }

@keyframes pulse-attention {
  0%, 100% { box-shadow: 0 6px 20px rgba(196, 30, 58, 0.3), 0 0 0 0 rgba(196, 30, 58, 0.5); }
  50%      { box-shadow: 0 6px 20px rgba(196, 30, 58, 0.3), 0 0 0 14px rgba(196, 30, 58, 0); }
}

.collapse-btn {
  position: absolute;
  top: 14px;
  right: 14px;
  width: 36px;
  height: 36px;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.12);
  border: 1px solid rgba(255, 255, 255, 0.15);
  color: white;
  cursor: pointer;
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 3;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.2s, transform 0.2s cubic-bezier(0.34, 1.56, 0.64, 1);
}
.collapse-btn:hover { background: rgba(255, 255, 255, 0.22); transform: scale(1.08); }
.collapse-btn:active { transform: scale(0.92); transition-duration: 0.08s; }

/* ============ CHAT ============ */
.chat {
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-left: 1px solid var(--border);
  min-height: 0;   /* permette al figlio messages di scrollare */
}
.chat-header {
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.chat-title h1 {
  margin: 0;
  font-size: 16px;
  font-weight: 700;
  color: var(--primary);
  letter-spacing: -0.01em;
}
.chat-title p {
  margin: 3px 0 0;
  color: var(--muted);
  font-size: 12px;
}
.show-avatar-btn {
  display: none;
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background: var(--primary);
  color: white;
  border: 0;
  cursor: pointer;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.2s, transform 0.2s cubic-bezier(0.34, 1.56, 0.64, 1), box-shadow 0.2s;
  box-shadow: 0 2px 8px rgba(27, 60, 110, 0.2);
}
.show-avatar-btn:hover { background: var(--primary-hover); transform: scale(1.06); box-shadow: 0 4px 12px rgba(27, 60, 110, 0.3); }
.show-avatar-btn:active { transform: scale(0.94); transition-duration: 0.08s; }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 20px;
  scroll-behavior: smooth;
  background: var(--bg);
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}
.messages::-webkit-scrollbar { width: 8px; }
.messages::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 4px; }
.messages::-webkit-scrollbar-track { background: transparent; }

.msg {
  margin-bottom: 12px;
  max-width: 88%;
  animation: msg-in 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}
@keyframes msg-in {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}
.msg.user {
  margin-left: auto;
  background: var(--primary);
  color: white;
  padding: 10px 14px;
  border-radius: 14px 14px 4px 14px;
  white-space: pre-wrap;
  word-wrap: break-word;
  overflow-wrap: anywhere;
  box-shadow: 0 2px 6px rgba(27, 60, 110, 0.15);
}
.msg.assistant {
  background: var(--panel-3);
  color: var(--text);
  padding: 11px 14px;
  border-radius: 14px 14px 14px 4px;
  border: 1px solid var(--border);
  overflow-wrap: anywhere;
}
.msg.assistant.streaming::after {
  content: "▊";
  display: inline-block;
  animation: blink 0.9s steps(1) infinite;
  color: var(--primary);
  margin-left: 2px;
}
@keyframes blink { 50% { opacity: 0; } }

.msg.assistant :first-child { margin-top: 0; }
.msg.assistant :last-child { margin-bottom: 0; }
.msg.assistant p { margin: 7px 0; }
.msg.assistant ul, .msg.assistant ol { margin: 7px 0; padding-left: 22px; }
.msg.assistant li { margin: 3px 0; }
.msg.assistant strong { font-weight: 600; color: var(--primary); }
.msg.assistant em { color: var(--muted); }
.msg.assistant a { color: var(--primary); text-decoration: underline; text-underline-offset: 2px; }
.msg.assistant a:hover { color: var(--primary-hover); }
.msg.assistant code {
  background: var(--code-bg);
  padding: 2px 6px;
  border-radius: 4px;
  font-family: "SF Mono", Menlo, Consolas, monospace;
  font-size: 12.5px;
  color: var(--accent);
  border: 1px solid var(--border);
}
.msg.assistant pre {
  background: #0F1B2D;
  padding: 10px 12px;
  border-radius: 8px;
  overflow-x: auto;
  margin: 8px 0;
}
.msg.assistant pre code { background: none; padding: 0; color: #E4EAF2; border: none; }
.msg.assistant h1, .msg.assistant h2, .msg.assistant h3, .msg.assistant h4 {
  margin: 10px 0 6px;
  font-weight: 600;
  color: var(--primary);
}
.msg.assistant h3 { font-size: 15px; }
.msg.assistant h4 { font-size: 14px; }
.msg.assistant table {
  border-collapse: collapse;
  margin: 8px 0;
  font-size: 13px;
  width: 100%;
  display: block;
  overflow-x: auto;
}
.msg.assistant th, .msg.assistant td { border: 1px solid var(--border-strong); padding: 5px 10px; text-align: left; }
.msg.assistant th { background: var(--primary); color: white; font-weight: 600; }
.msg.assistant blockquote {
  margin: 8px 0;
  padding: 4px 12px;
  border-left: 3px solid var(--primary);
  color: var(--muted);
  background: rgba(27, 60, 110, 0.04);
}

.thinking {
  display: inline-flex;
  gap: 5px;
  align-items: center;
  padding: 12px 14px;
  background: var(--panel-3);
  border: 1px solid var(--border);
  border-radius: 14px 14px 14px 4px;
  margin-bottom: 12px;
}
.thinking span {
  width: 7px;
  height: 7px;
  background: var(--primary);
  border-radius: 50%;
  animation: bounce 1.2s infinite ease-in-out;
  opacity: 0.6;
}
.thinking span:nth-child(1) { animation-delay: -0.32s; }
.thinking span:nth-child(2) { animation-delay: -0.16s; }
@keyframes bounce { 0%,80%,100%{transform:scale(.7);opacity:.4} 40%{transform:scale(1);opacity:1} }

.tool-badge {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: var(--primary);
  padding: 6px 11px;
  background: rgba(27, 60, 110, 0.06);
  border: 1px solid rgba(27, 60, 110, 0.15);
  border-radius: 8px;
  margin: 6px 0;
  font-style: italic;
}
.tool-badge.running .spinner {
  width: 12px; height: 12px;
  border: 2px solid transparent;
  border-top-color: var(--primary);
  border-right-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
.tool-badge {
  animation: tool-fade-in 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}
.tool-badge.done {
  color: #15803D;
  background: rgba(21, 128, 61, 0.06);
  border-color: rgba(21, 128, 61, 0.2);
  transition: color 0.25s, background 0.25s, border-color 0.25s;
}
.tool-badge.done .spinner {
  animation: none;
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
}
.tool-badge.done .spinner::before {
  content: "✓";
  color: #15803D;
  font-style: normal;
  font-weight: 700;
  font-size: 14px;
  animation: checkmark-pop 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}
@keyframes tool-fade-in {
  from { opacity: 0; transform: translateY(-4px); }
  to { opacity: 1; transform: translateY(0); }
}
@keyframes checkmark-pop {
  0% { transform: scale(0); opacity: 0; }
  60% { transform: scale(1.25); opacity: 1; }
  100% { transform: scale(1); }
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ============ INPUT ============ */
.input-wrap {
  display: flex;
  gap: 8px;
  padding: 12px 16px max(12px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  background: var(--panel);
  align-items: flex-end;
}
#input {
  flex: 1;
  background: var(--bg);
  border: 1px solid var(--border-strong);
  color: var(--text);
  padding: 10px 14px;
  border-radius: 12px;
  font-size: 16px;        /* 16px evita zoom iOS Safari al focus */
  font-family: inherit;
  line-height: 1.4;
  resize: none;
  max-height: 140px;
  min-height: 42px;
  outline: none;
  overflow-y: auto;       /* scroll interno quando supera max-height */
  overscroll-behavior: contain;
  transition: border-color 0.15s, box-shadow 0.15s;
}
#input::placeholder { color: var(--muted); }
#input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(27, 60, 110, 0.1); }
#input::-webkit-scrollbar { width: 6px; }
#input::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 3px; }

.send-btn {
  position: relative;
  background: var(--primary);
  color: white;
  border: 0;
  padding: 0 14px;
  border-radius: 12px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 48px;
  height: 42px;
  transition:
    background 0.2s cubic-bezier(0.4, 0, 0.2, 1),
    transform 0.18s cubic-bezier(0.34, 1.56, 0.64, 1),
    box-shadow 0.2s cubic-bezier(0.4, 0, 0.2, 1);
  box-shadow: 0 2px 8px rgba(27, 60, 110, 0.25);
  flex-shrink: 0;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}
.send-btn::before {
  /* Ripple effect */
  content: "";
  position: absolute;
  inset: 0;
  background: radial-gradient(circle at center, rgba(255,255,255,0.35) 0%, transparent 60%);
  opacity: 0;
  transition: opacity 0.35s;
  pointer-events: none;
}
.send-btn:hover { background: var(--primary-hover); transform: translateY(-1px); box-shadow: 0 6px 16px rgba(27, 60, 110, 0.35); }
.send-btn:active { transform: translateY(0) scale(0.94); transition-duration: 0.08s; }
.send-btn:active::before { opacity: 1; transition-duration: 0.1s; }
.send-btn:disabled { opacity: 0.5; cursor: not-allowed; transform: none; box-shadow: 0 2px 6px rgba(27, 60, 110, 0.15); }
.send-btn svg { transition: transform 0.18s cubic-bezier(0.34, 1.56, 0.64, 1); }
.send-btn:hover .icon-send { transform: translate(1px, -1px); }

.send-btn.stopping {
  background: var(--accent);
  box-shadow: 0 2px 8px rgba(196, 30, 58, 0.3);
  animation: stop-pulse 1.2s ease-in-out infinite;
}
.send-btn.stopping:hover { background: var(--accent-hover); box-shadow: 0 6px 16px rgba(196, 30, 58, 0.4); }
@keyframes stop-pulse {
  0%, 100% { box-shadow: 0 2px 8px rgba(196, 30, 58, 0.3); }
  50%      { box-shadow: 0 2px 8px rgba(196, 30, 58, 0.3), 0 0 0 4px rgba(196, 30, 58, 0.15); }
}

/* ============ TABLET ============ */
@media (max-width: 900px) {
  .wrap { grid-template-columns: 1fr; grid-template-rows: 38vh 1fr; }
}

/* ============ MOBILE ============ */
@media (max-width: 640px) {
  .wrap {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr;  /* solo chat per default, avatar overlay */
  }

  /* Avatar panel come overlay: nascosto di default, visibile con .avatar-open */
  .avatar {
    position: fixed;
    inset: 0;
    z-index: 100;
    transform: translateY(100%);
    transition: transform 0.25s ease-out;
  }
  body.avatar-open .avatar {
    transform: translateY(0);
  }
  .collapse-btn { display: inline-flex; }
  .show-avatar-btn { display: inline-flex; }

  .chat-header { padding: 12px 16px; }
  .chat-title h1 { font-size: 15px; }
  .chat-title p { font-size: 11px; }

  .messages { padding: 14px 14px; }
  .msg { max-width: 92%; }
  .msg.user, .msg.assistant { padding: 9px 12px; font-size: 14px; }

  .input-wrap { padding: 10px 12px max(10px, env(safe-area-inset-bottom)); gap: 6px; }
  #input { padding: 10px 12px; max-height: 120px; }
  .send-btn { min-width: 44px; height: 40px; padding: 0 12px; }

  .video-placeholder { width: min(85%, 320px); aspect-ratio: 3/4; }
  .start-btn { bottom: 24px; padding: 12px 22px; font-size: 13px; }
}
