/* ── Reset & Base ─────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:          #080c18;
  --bg-surface:  #0f1628;
  --bg-elevated: #161e35;
  --accent:      #00b4d8;
  --accent-dim:  rgba(0, 180, 216, 0.15);
  --green:       #00e676;
  --yellow:      #ffb700;
  --red:         #ff4444;
  --text:        #e8eaf6;
  --text-dim:    #7986a8;
  --border:      rgba(255, 255, 255, 0.08);
  --radius:      16px;
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  font-size: 16px;
  line-height: 1.5;
  overflow: hidden;
}

/* ── Layout ───────────────────────────────────────────────────────── */
#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  max-width: 600px;
  margin: 0 auto;
  position: relative;
}

/* ── Header ───────────────────────────────────────────────────────── */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px 12px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.logo {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 18px;
  font-weight: 700;
  letter-spacing: -0.3px;
}

.logo-cross {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  background: var(--accent);
  color: var(--bg);
  border-radius: 6px;
  font-size: 18px;
  font-weight: 900;
  line-height: 1;
}

.logo-ai { color: var(--accent); }

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

/* ── Main ─────────────────────────────────────────────────────────── */
.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 16px 20px;
  gap: 16px;
  overflow: hidden;
}

/* ── Avatar ───────────────────────────────────────────────────────── */
.avatar-wrap {
  position: relative;
  /* Responsive: grande su totem portrait, si restringe su schermi piccoli */
  width:  clamp(220px, 55vw, 320px);
  height: clamp(220px, 55vw, 320px);
  flex-shrink: 0;
}

.avatar-ring {
  position: absolute;
  inset: -6px;
  border-radius: 50%;
  border: 2px solid transparent;
  pointer-events: none;
  transition: border-color 0.4s, box-shadow 0.4s;
}

.avatar-circle {
  position: relative;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background: #0d1424;
  border: 2px solid rgba(255,255,255,0.06);
  overflow: hidden;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  transition: box-shadow 0.4s;
}

.avatar-img {
  width: 105%;
  height: 105%;
  object-fit: cover;
  object-position: center top;
  pointer-events: none;
  user-select: none;
}

/* Avatar state: idle */
.avatar-wrap.idle .avatar-circle {
  box-shadow: 0 0 28px rgba(0,180,216,0.2), 0 0 70px rgba(0,180,216,0.06);
  animation: idle-breathe 4s ease-in-out infinite;
}
.avatar-wrap.idle .avatar-ring { border-color: rgba(0,180,216,0.25); }

/* Avatar state: listening */
.avatar-wrap.listening .avatar-circle {
  box-shadow: 0 0 36px rgba(0,230,118,0.55), 0 0 90px rgba(0,230,118,0.18);
  animation: listen-pulse 0.9s ease-in-out infinite;
}
.avatar-wrap.listening .avatar-ring {
  border-color: rgba(0,230,118,0.55);
  animation: ring-spin 2s linear infinite;
}

/* Avatar state: thinking */
.avatar-wrap.thinking .avatar-circle {
  box-shadow: 0 0 36px rgba(255,183,0,0.45), 0 0 90px rgba(255,183,0,0.12);
  animation: think-flicker 1.5s ease-in-out infinite;
}
.avatar-wrap.thinking .avatar-ring {
  border-color: rgba(255,183,0,0.55);
  border-top-color: transparent;
  animation: ring-spin 1s linear infinite;
}

/* Avatar state: speaking */
.avatar-wrap.speaking .avatar-circle {
  box-shadow: 0 0 44px rgba(0,180,216,0.65), 0 0 110px rgba(0,180,216,0.22);
  animation: speak-wave 0.45s ease-in-out infinite alternate;
}
.avatar-wrap.speaking .avatar-ring {
  border-color: rgba(0,180,216,0.65);
  animation: ring-spin 0.8s linear infinite;
}

/* Avatar state: error */
.avatar-wrap.error .avatar-circle {
  box-shadow: 0 0 32px rgba(255,68,68,0.45), 0 0 80px rgba(255,68,68,0.12);
}
.avatar-wrap.error .avatar-ring { border-color: rgba(255,68,68,0.55); }

/* Keyframes */
@keyframes idle-breathe {
  0%, 100% { transform: scale(1); }
  50%       { transform: scale(1.015); }
}
@keyframes listen-pulse {
  0%, 100% { transform: scale(1); }
  50%       { transform: scale(1.03); }
}
@keyframes think-flicker {
  0%, 100% { opacity: 1; }
  50%       { opacity: 0.88; }
}
@keyframes speak-wave {
  from { transform: scale(1); }
  to   { transform: scale(1.04); }
}
@keyframes ring-spin {
  from { transform: rotate(0deg); }
  to   { transform: rotate(360deg); }
}

/* ── Status text ──────────────────────────────────────────────────── */
.status {
  font-size: 13px;
  color: var(--text-dim);
  letter-spacing: 0.5px;
  height: 18px;
  transition: color 0.3s;
  text-align: center;
}
.status.listening { color: var(--green); }
.status.thinking  { color: var(--yellow); }
.status.speaking  { color: var(--accent); }
.status.error     { color: var(--red); }

/* ── Controls ─────────────────────────────────────────────────────── */
.controls {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.mic-btn {
  width: 68px;
  height: 68px;
  border-radius: 50%;
  border: none;
  background: var(--bg-elevated);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s, box-shadow 0.2s, transform 0.1s;
  box-shadow: 0 4px 20px rgba(0,0,0,0.4), inset 0 1px 0 rgba(255,255,255,0.06);
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  touch-action: none;
}
.mic-btn:hover { background: #1e2a45; }
.mic-btn.recording {
  background: rgba(255,68,68,0.15);
  transform: scale(0.96);
  animation: mic-ring 1s ease-out infinite;
}
.mic-btn.locked {
  background: rgba(0,230,118,0.1);
  box-shadow: 0 0 24px rgba(0,230,118,0.3);
}
.mic-btn.disabled { opacity: 0.4; cursor: not-allowed; }

@keyframes mic-ring {
  0%   { box-shadow: 0 0 0 0 rgba(255,68,68,0.5); }
  70%  { box-shadow: 0 0 0 16px rgba(255,68,68,0); }
  100% { box-shadow: 0 0 0 0 rgba(255,68,68,0); }
}

.mic-icon { pointer-events: none; }

.mic-hint {
  font-size: 11px;
  color: var(--text-dim);
  text-align: center;
  max-width: 240px;
  line-height: 1.4;
}

/* ── Text input ───────────────────────────────────────────────────── */
.text-input-container {
  width: 100%;
  max-width: 480px;
  display: flex;
  gap: 8px;
  animation: slide-in 0.2s ease;
}
.text-input-container.hidden { display: none; }

@keyframes slide-in {
  from { opacity: 0; transform: translateY(-8px); }
  to   { opacity: 1; transform: translateY(0); }
}

.text-input {
  flex: 1;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 16px;
  color: var(--text);
  font-size: 14px;
  outline: none;
  transition: border-color 0.2s;
}
.text-input:focus { border-color: rgba(0,180,216,0.4); }
.text-input::placeholder { color: var(--text-dim); }

.send-btn {
  background: var(--accent);
  color: var(--bg);
  border: none;
  border-radius: 12px;
  padding: 10px 18px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.2s;
  white-space: nowrap;
}
.send-btn:hover { opacity: 0.85; }
.send-btn:disabled { opacity: 0.4; cursor: not-allowed; }

/* ── Icon button ──────────────────────────────────────────────────── */
.icon-btn {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text-dim);
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: 16px;
  transition: color 0.2s, border-color 0.2s;
}
.icon-btn:hover { color: var(--text); border-color: rgba(255,255,255,0.15); }
.icon-btn.active { color: var(--accent); border-color: var(--accent); }

/* ── Toast ────────────────────────────────────────────────────────── */
.toast {
  position: fixed;
  top: 20px;
  left: 50%;
  transform: translateX(-50%) translateY(-80px);
  background: var(--bg-elevated);
  border: 1px solid var(--red);
  color: var(--text);
  padding: 12px 20px;
  border-radius: var(--radius);
  font-size: 14px;
  max-width: 360px;
  text-align: center;
  transition: transform 0.3s ease;
  z-index: 1000;
  box-shadow: 0 8px 32px rgba(0,0,0,0.5);
}
.toast.show { transform: translateX(-50%) translateY(0); }

/* ── Transcript panel ─────────────────────────────────────────────── */
.transcript-panel {
  position: fixed;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 100%;
  max-width: 600px;
  height: 50vh;
  background: var(--bg-surface);
  border-top: 1px solid var(--border);
  border-radius: 20px 20px 0 0;
  display: flex;
  flex-direction: column;
  z-index: 100;
  transition: transform 0.3s ease;
}
.transcript-panel.hidden {
  transform: translateX(-50%) translateY(100%);
}

.transcript-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
  font-weight: 600;
  flex-shrink: 0;
}

.transcript-content {
  flex: 1;
  overflow-y: auto;
  padding: 16px 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}

.transcript-empty {
  color: var(--text-dim);
  font-size: 13px;
  text-align: center;
  padding: 24px 0;
}

.transcript-entry {
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.transcript-entry .role {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.8px;
  text-transform: uppercase;
  opacity: 0.5;
}
.transcript-entry.user .role      { color: var(--green); }
.transcript-entry.assistant .role { color: var(--accent); }
.transcript-entry .content {
  font-size: 13px;
  line-height: 1.6;
  color: var(--text);
}

/* ── Sources in transcript ────────────────────────────────────────── */
.transcript-sources {
  margin-top: 6px;
  padding-top: 6px;
  border-top: 1px solid var(--border);
}
.transcript-sources-label {
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.6px;
  text-transform: uppercase;
  color: var(--text-dim);
  margin-bottom: 4px;
}
.transcript-source-link {
  display: flex;
  flex-direction: column;
  gap: 1px;
  margin-bottom: 6px;
}
.transcript-source-link a {
  color: var(--accent);
  text-decoration: none;
  font-size: 12px;
  line-height: 1.4;
  word-break: break-word;
}
.transcript-source-link a:hover { text-decoration: underline; }
.transcript-source-meta {
  font-size: 11px;
  color: var(--text-dim);
}

/* ── Footer ───────────────────────────────────────────────────────── */
.footer {
  padding: 10px 20px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
  gap: 12px;
}

.transcript-toggle-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text-dim);
  padding: 6px 12px;
  font-size: 12px;
  cursor: pointer;
  transition: color 0.2s, border-color 0.2s;
  white-space: nowrap;
}
.transcript-toggle-btn:hover { color: var(--text); border-color: rgba(255,255,255,0.15); }
.transcript-toggle-btn.active { color: var(--accent); border-color: var(--accent); }

.disclaimer {
  font-size: 10px;
  color: var(--text-dim);
  opacity: 0.6;
  text-align: right;
  flex: 1;
  line-height: 1.3;
}

/* ── Confirm dialog ───────────────────────────────────────────────── */
.confirm-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 500;
  backdrop-filter: blur(4px);
}
.confirm-overlay.hidden { display: none; }

.confirm-dialog {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  max-width: 300px;
  width: 90%;
  text-align: center;
  box-shadow: 0 16px 64px rgba(0,0,0,0.6);
}
.confirm-dialog p {
  font-size: 15px;
  margin-bottom: 20px;
  line-height: 1.5;
}
.confirm-dialog small {
  font-size: 12px;
  color: var(--text-dim);
}
.confirm-actions {
  display: flex;
  gap: 10px;
  justify-content: center;
}
.btn-secondary {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  color: var(--text-dim);
  border-radius: 10px;
  padding: 9px 18px;
  font-size: 14px;
  cursor: pointer;
  transition: color 0.2s;
}
.btn-secondary:hover { color: var(--text); }
.btn-primary {
  background: var(--accent);
  border: none;
  color: var(--bg);
  border-radius: 10px;
  padding: 9px 18px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.2s;
}
.btn-primary:hover { opacity: 0.85; }

/* ── Scrollbar ────────────────────────────────────────────────────── */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }
