/* ═══════════════════════════════════════════════════════════
   simple.css — layout chat full-screen verticale (UI v2, ex simple-v2.css
   post migrazione Fase 5 / sess 12).

   Consuma i token globali da tokens.css (Fase 3). Qui restano solo i
   token di LAYOUT specifici di questa UI + i derivati semantici di
   bubble (--msg-*) che dipendono dai globali ma variano per ruolo.

   Scope tema unificato (sess 11): [data-theme="auto|light|dark|gold"].
   Precedente "theme3" viola rimosso, sostituito da "gold" più caratterizzato.
   I --msg-* derivano da var(--accent, --bg-2, --text, --text-dim) → seguono
   il tema senza ridefinizione esplicita.

   Invarianti: vanilla CSS, nessun framework, nessuna dipendenza da
   style.css v1, classi .sv-* invariate.
═══════════════════════════════════════════════════════════ */

:root {
  /* Layout tokens (specifici simple.v2, non globalizzabili) */
  --header-h:    56px;
  --input-pad:   120px;
  --chat-max:    880px;
  --bubble-min:  560px;
  --drawer-w:    280px;

  /* Message bubble derivatives (cambiano con --accent/--bg-2/--text/--text-dim
     in tokens.css → seguono il tema attivo senza ridefinizione). */
  --msg-user-bg:          var(--accent);
  --msg-user-text:        var(--accent-text);
  --msg-assistant-bg:     var(--bg-2);
  --msg-assistant-text:   var(--text);
  --msg-thinking-bg:      var(--bg-2);
  --msg-thinking-text:    var(--text-dim);
}

* { box-sizing: border-box; }
* { scrollbar-width: thin; scrollbar-color: var(--border) transparent; }
*::-webkit-scrollbar { width: 6px; height: 6px; }
*::-webkit-scrollbar-track { background: transparent; }
*::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
*::-webkit-scrollbar-thumb:hover { background: var(--text-dim); }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-body);
  font-size: var(--fs-base);
  overflow: hidden;
  overscroll-behavior: none;
}

/* ──── Shell layout ──── */
.sv-app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  min-height: 100vh;
  min-height: 100dvh;
}

.sv-header {
  height: var(--header-h);
  padding: 0 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 30;
}

.sv-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.sv-brand svg { width: 22px; height: 22px; }

.sv-brand-tag {
  font-size: 0.72rem;
  color: var(--text-muted);
  background: var(--bg-2);
  padding: 2px 8px;
  border-radius: 6px;
  margin-left: 4px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.sv-header-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

.sv-header-link {
  font-size: 0.78rem;
  color: var(--text-dim);
  text-decoration: none;
  padding: 6px 10px;
  border-radius: 6px;
  transition: background 0.15s, color 0.15s;
}

.sv-header-link:hover { background: var(--bg-2); color: var(--text); }

.sv-iconbtn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--bg-2);
  color: var(--text);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
}

.sv-iconbtn:hover { background: var(--surface); }

.sv-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-text);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 0.88rem;
  cursor: pointer;
  overflow: hidden;
  border: none;
}

.sv-avatar img { width: 100%; height: 100%; object-fit: cover; }

/* ──── Drawer sessioni ──── */
.sv-drawer {
  position: fixed;
  top: var(--header-h);
  left: 0;
  bottom: 0;
  width: var(--drawer-w);
  background: var(--surface);
  border-right: 1px solid var(--border);
  transform: translateX(-100%);
  transition: transform 0.25s ease;
  z-index: 20;
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow-xl);
}

.sv-drawer.open { transform: translateX(0); }

.sv-drawer-head {
  padding: 14px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border);
}

.sv-drawer-title {
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text-dim);
  letter-spacing: 0.02em;
}

.sv-drawer-actions { display: flex; gap: 6px; }

.sv-drawer-btn {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 5px 10px;
  color: var(--text-dim);
  font-size: 0.82rem;
  cursor: pointer;
}

.sv-drawer-btn:hover { background: var(--bg-2); color: var(--text); }

.sv-sessions-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px 10px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.sv-session-item {
  /* Altezza minima + padding ampio + line-height generoso:
     garantiscono che la content-box sia sempre più alta del testo
     (ascender "h"/"l" + descender "p"/"g" non vengono mai tagliati
     da overflow: hidden, indipendentemente dalle metriche del font). */
  padding: 12px 14px;
  min-height: 44px;
  box-sizing: border-box;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.88rem;
  line-height: 1.55;
  color: var(--text-dim);
  border: 1px solid transparent;
  transition: background 0.15s, color 0.15s, border 0.15s;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sv-session-item:hover { background: var(--bg-2); color: var(--text); }

.sv-session-item.active {
  background: var(--bg-2);
  color: var(--text);
  border-color: var(--border);
}

/* ──── Chat area ──── */
.sv-main {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  position: relative;
}

.sv-chatbox {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 20px 24px var(--input-pad);
}

.sv-chatbox-inner {
  max-width: var(--chat-max);
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

/* ──── Bubbles ──── */
.msg {
  position: relative;
  padding: 12px 16px;
  border-radius: var(--radius-lg);
  word-wrap: break-word;
  line-height: 1.55;
  animation: sv-bubble-in 0.25s ease-out;
}

@keyframes sv-bubble-in {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: none; }
}

.msg.msg-user {
  align-self: flex-end;
  background: var(--msg-user-bg);
  color: var(--msg-user-text);
  max-width: 78%;
}

.msg.msg-assistant {
  align-self: flex-start;
  background: var(--msg-assistant-bg);
  color: var(--msg-assistant-text);
  min-width: var(--bubble-min);
  max-width: 86%;
  border: 1px solid var(--border);
}

.msg.msg-thinking {
  align-self: flex-start;
  background: var(--msg-thinking-bg);
  color: var(--msg-thinking-text);
  padding: 10px 16px;
  display: inline-flex;
  gap: 4px;
  max-width: fit-content;
}

.thinking-dot {
  width: 7px;
  height: 7px;
  background: currentColor;
  border-radius: 50%;
  opacity: 0.5;
  animation: sv-dot 1.2s infinite;
}

.thinking-dot:nth-child(2) { animation-delay: 0.2s; }
.thinking-dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes sv-dot {
  0%, 60%, 100% { opacity: 0.3; transform: none; }
  30% { opacity: 1; transform: translateY(-3px); }
}

.msg-text { white-space: pre-wrap; }

.msg-copy-btn {
  position: absolute;
  right: 6px;
  bottom: -28px;
  background: transparent;
  border: 1px solid transparent;
  color: var(--text-muted);
  font-size: 0.72rem;
  padding: 3px 8px;
  border-radius: 6px;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.15s, color 0.15s;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.msg.msg-assistant:hover .msg-copy-btn { opacity: 0.85; }

.msg-copy-btn:hover { color: var(--text); border-color: var(--border); }

.msg-tool-indicator {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--text-dim);
  font-size: 0.88rem;
  font-style: italic;
}

/* ──── Input area ──── */
.sv-input-wrap {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 12px 24px calc(18px + env(safe-area-inset-bottom));
  background: linear-gradient(to bottom, transparent, var(--bg) 30%);
  pointer-events: none;
  z-index: 10;
}

.sv-input-inner {
  max-width: var(--chat-max);
  margin: 0 auto;
  pointer-events: auto;
}

.sv-usage-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 0.74rem;
  color: var(--text-muted);
  margin-bottom: 6px;
  padding: 0 4px;
}

.sv-usage-track {
  flex: 1;
  height: 3px;
  background: var(--bg-2);
  border-radius: 3px;
  overflow: hidden;
}

.sv-usage-fill {
  height: 100%;
  background: var(--accent);
  transition: width 0.3s;
}

.sv-input-box {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-2xl);
  padding: 8px 8px 8px 16px;
  box-shadow: var(--shadow-sm);
  transition: border-color 0.15s, box-shadow 0.15s;
}

.sv-input-box:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(0,0,0,0.04);
}

.sv-textarea {
  flex: 1;
  border: none;
  outline: none;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 0.95rem;
  resize: none;
  line-height: 1.4;
  max-height: 180px;
  min-height: 22px;
  padding: 8px 4px;
  font-family: inherit;
}

.sv-send {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-text);
  border: none;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s, opacity 0.15s;
}

.sv-send:hover:not(:disabled) { background: var(--accent-hover); }
.sv-send:disabled { opacity: 0.5; cursor: not-allowed; }

/* ──── Microfono Web Speech (sess 12+) ──────────────────────
   Click → toggle registrazione; durante .recording il bottone diventa rosso
   e mostra due onde concentriche pulsanti (::before/::after sfasati) +
   un pulse del box-shadow del bottone stesso = 3 livelli di onde sonore.
   La trascrizione vive in JS (simple.js setupMic), Web Speech API. */
.sv-mic {
  position: relative;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-dim);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}

.sv-mic:hover {
  background: var(--bg-2);
  color: var(--text);
  border-color: var(--accent);
}

.sv-mic.recording {
  background: #c14b4b;
  border-color: #c14b4b;
  color: #fff;
  animation: sv-mic-pulse 1.6s ease-out infinite;
}

/* ──── Account popup ──── */
.sv-popup {
  position: absolute;
  top: calc(var(--header-h) + 6px);
  right: 18px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 14px;
  min-width: 260px;
  box-shadow: var(--shadow-xl);
  z-index: 40;
  display: none;
}

.sv-popup.open { display: block; }

/* ──── Tools popup (sess 12+) ──────────────────────────────
   Variante .sv-tools-popup: ancorata a sinistra (sotto al toggle nell'header)
   anziché a destra come l'account popup. Stesso layer/ombra/border.
   Padding ridotto perché contiene solo 3 voci, no header utente.            */
.sv-tools-popup {
  left: 18px;
  right: auto;
  padding: 6px;
  min-width: 240px;
}

.sv-tools-item {
  /* Eredita .sv-popup-btn (display flex, gap, padding, hover bg-2).
     Aggiungo solo il colore icona dim → text al hover, per coerenza. */
  color: var(--text);
}

.sv-tools-item svg {
  color: var(--text-dim);
  flex-shrink: 0;
  transition: color 0.15s;
}

.sv-tools-item:hover svg { color: var(--accent); }

/* ──── Tool panel fullscreen (Gmail/Calendar/Drive) ─────────
   Container fisso sotto l'header. Bottom: 0 ma il body interno ha
   padding-bottom = --input-pad → contenuti non finiscono mai sotto la
   input bar (che resta visibile/cliccabile sopra al pannello, z=10).
   Il drawer conversazioni (z=20) si sovrappone al pannello quando aperto. */
.sv-tool-panel {
  position: fixed;
  top: var(--header-h);
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--bg);
  z-index: 5;
  display: none;
  flex-direction: column;
  animation: sv-tool-fade 0.18s ease-out;
}

.sv-tool-panel.open { display: flex; }

@keyframes sv-tool-fade {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: none; }
}

.sv-tool-panel-head {
  flex-shrink: 0;
  padding: 14px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.sv-tool-panel-title {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 1.05rem;
  font-weight: 600;
  letter-spacing: -0.01em;
  color: var(--text);
}

.sv-tool-panel-icon {
  display: inline-flex;
  width: 22px;
  height: 22px;
  align-items: center;
  justify-content: center;
  color: var(--accent);
}

.sv-tool-panel-icon svg { width: 100%; height: 100%; }

.sv-tool-panel-close {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-dim);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, color 0.15s;
}

.sv-tool-panel-close:hover { background: var(--bg-2); color: var(--text); }

.sv-tool-panel-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  /* padding-bottom = --input-pad: lascia respiro per la input bar fissa */
  padding: 20px 24px var(--input-pad);
}

.sv-tool-panel-body > .sv-tool-list,
.sv-tool-panel-body > .sv-tool-panel-empty,
.sv-tool-panel-body > .sv-tool-panel-loading,
.sv-tool-panel-body > .sv-tool-panel-error {
  max-width: var(--chat-max);
  margin: 0 auto;
}

.sv-tool-panel-loading,
.sv-tool-panel-empty,
.sv-tool-panel-error {
  text-align: center;
  padding: 40px 20px;
  color: var(--text-dim);
  font-size: 0.92rem;
}

.sv-tool-panel-error { color: var(--danger); }

/* ──── Lista item dentro il tool panel ──── */
.sv-tool-list { display: flex; flex-direction: column; gap: 8px; }

.sv-tool-list-item {
  display: block;
  padding: 12px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  text-decoration: none;
  color: inherit;
  transition: background 0.15s, border-color 0.15s;
  cursor: pointer;
}

.sv-tool-list-item:hover {
  background: var(--bg-2);
  border-color: var(--accent-soft);
}

.sv-tool-list-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
}

.sv-tool-list-title {
  font-weight: 500;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
  min-width: 0;
}

.sv-tool-list-item.unread .sv-tool-list-title { font-weight: 600; }

.sv-tool-list-meta {
  font-size: 0.78rem;
  color: var(--text-muted);
  flex-shrink: 0;
}

.sv-tool-list-sub {
  font-size: 0.85rem;
  color: var(--text-dim);
  margin-top: 4px;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.sv-tool-list-from {
  font-size: 0.8rem;
  color: var(--text-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 2px;
}

.sv-popup-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding-bottom: 10px;
  margin-bottom: 10px;
  border-bottom: 1px solid var(--border);
}

.sv-popup-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-text);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  overflow: hidden;
}

.sv-popup-avatar img { width: 100%; height: 100%; object-fit: cover; }

.sv-popup-name {
  font-weight: 600;
  font-size: 0.92rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 170px;
}

.sv-popup-email {
  font-size: 0.78rem;
  color: var(--text-dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 170px;
}

.sv-popup-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 8px 10px;
  background: transparent;
  border: none;
  border-radius: 8px;
  color: var(--text);
  text-align: left;
  cursor: pointer;
  font-size: 0.86rem;
  font-family: inherit;
}

.sv-popup-btn:hover { background: var(--bg-2); }

.sv-popup-btn--danger { color: #d94343; }
.sv-popup-btn--danger:hover { background: rgba(217, 67, 67, 0.08); }

.sv-popup-divider {
  height: 1px;
  background: var(--border);
  margin: 8px -14px;
  border: none;
}

/* ──── Theme picker (sess 11) ──────────────────────────────
   4 bottoni Auto/Light/Dark/Gold dentro il popup account.
   Il JS applica data-theme su <html> e persiste in localStorage. */
.sv-theme-picker {
  margin-top: 4px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--bg-2);
}

.sv-theme-picker-label {
  font-size: var(--fs-xs);
  color: var(--text-muted);
  letter-spacing: var(--ls-wide);
  text-transform: uppercase;
  margin-bottom: 6px;
  font-weight: var(--fw-medium);
}

.sv-theme-picker-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 4px;
}

.sv-theme-btn {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text-dim);
  font-family: inherit;
  font-size: var(--fs-xs);
  padding: 5px 6px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}

.sv-theme-btn:hover {
  background: var(--accent-soft);
  color: var(--text);
  border-color: var(--accent);
}

.sv-theme-btn.is-active {
  background: var(--accent-soft);
  color: var(--text);
  border-color: var(--accent);
}

/* ──── Auth overlay (welcome prima del login) ──── */
.sv-auth {
  position: fixed;
  inset: 0;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 20px;
  padding: 32px;
  z-index: 100;
}

.sv-auth h2 {
  font-size: 1.6rem;
  font-weight: 600;
  margin: 0;
  letter-spacing: -0.02em;
  color: var(--text);
}

.sv-auth p {
  color: var(--text-dim);
  text-align: center;
  max-width: 420px;
  margin: 0;
  font-size: 0.92rem;
  line-height: 1.5;
}

.sv-auth-google {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  padding: 12px 20px;
  border-radius: var(--radius-lg);
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  font-weight: 500;
  font-size: 0.92rem;
  cursor: pointer;
  text-decoration: none;
  transition: background 0.15s;
}

.sv-auth-google:hover { background: var(--bg-2); }

/* ──── Empty state welcome ──── */
.sv-welcome {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 80px 20px 40px;
  color: var(--text-dim);
  gap: 12px;
}

.sv-welcome-title {
  font-size: 1.4rem;
  font-weight: 600;
  color: var(--text);
  letter-spacing: -0.01em;
}

.sv-welcome-sub {
  font-size: 0.92rem;
  max-width: 480px;
  line-height: 1.5;
}

.sv-welcome-hints {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: center;
  margin-top: 16px;
}

.sv-welcome-hint {
  padding: 8px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 0.82rem;
  cursor: pointer;
  color: var(--text-dim);
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}

.sv-welcome-hint:hover {
  background: var(--bg-2);
  color: var(--text);
  border-color: var(--accent);
}

/* ──── Responsive ──── */
@media (max-width: 768px) {
  :root {
    --bubble-min: auto;
    --chat-max: 100%;
  }
  .sv-chatbox { padding: 16px 14px var(--input-pad); }
  .sv-input-wrap { padding: 10px 14px calc(14px + env(safe-area-inset-bottom)); }
  .msg.msg-assistant { min-width: auto; max-width: 96%; }
  .msg.msg-user { max-width: 90%; }
  .sv-drawer { width: 86vw; max-width: 320px; }
  /* iOS Safari zoomma al focus su input con font-size < 16px → forziamo 16px
     sulla textarea (e su qualsiasi input) per disabilitare l'auto-zoom. */
  .sv-textarea { font-size: 16px; }
}

@media (max-width: 560px) {
  .sv-header { padding: 0 14px; }
  .sv-brand-tag { display: none; }
}

/* ──── Mic onde sonore ──── */
.sv-mic.recording::before,
.sv-mic.recording::after {
  content: "";
  position: absolute;
  inset: -2px;
  border-radius: 50%;
  border: 2px solid #c14b4b;
  pointer-events: none;
  animation: sv-mic-wave 1.6s ease-out infinite;
}

.sv-mic.recording::after { animation-delay: 0.55s; }

@keyframes sv-mic-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(193, 75, 75, 0.4); }
  50% { box-shadow: 0 0 0 4px rgba(193, 75, 75, 0.15); }
}

@keyframes sv-mic-wave {
  0%   { transform: scale(1);   opacity: 0.7; }
  100% { transform: scale(2.2); opacity: 0;   }
}

/* ──── Hidden utility ──── */
.hidden { display: none !important; }
