/* =========================================================
   Nos Ai — foglio di stile
   Tema scuro, nessuna dipendenza esterna.
   ========================================================= */

:root {
  /* Superfici */
  --bg: #0a0b10;
  --bg-2: #0e1016;
  --sidebar: #0c0e14;
  --panel: #14171f;
  --panel-2: #1a1e28;
  --panel-3: #222736;
  --hover: rgba(255, 255, 255, 0.045);
  --active: rgba(255, 255, 255, 0.075);
  --border: rgba(255, 255, 255, 0.07);
  --border-strong: rgba(255, 255, 255, 0.12);

  /* Testo */
  --text: #eceef4;
  --text-2: #b9bfcc;
  --muted: #7c8497;

  /* Accento (personalizzabile da Impostazioni → Aspetto) */
  --accent: #a78bfa;
  --accent-2: #f0abfc;
  --accent-rgb: 167, 139, 250;
  --grad: linear-gradient(135deg, var(--accent), var(--accent-2));

  /* Stato */
  --danger: #f87171;
  --danger-rgb: 248, 113, 113;
  --success: #34d399;
  --warning: #fbbf24;

  /* Forma */
  --radius-xs: 6px;
  --radius-sm: 10px;
  --radius: 14px;
  --radius-lg: 20px;
  --radius-xl: 26px;
  --sidebar-w: 280px;
  --content-w: 780px;

  --font: "Inter", "Segoe UI Variable Text", "Segoe UI", system-ui, -apple-system, "SF Pro Text", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: "Cascadia Code", "JetBrains Mono", "SF Mono", Consolas, "Liberation Mono", ui-monospace, monospace;

  --ease: cubic-bezier(0.22, 1, 0.36, 1);
  --ease-spring: cubic-bezier(0.34, 1.56, 0.64, 1);
  --shadow: 0 18px 50px -12px rgba(0, 0, 0, 0.7);
  --shadow-sm: 0 4px 16px -4px rgba(0, 0, 0, 0.5);

  /* Token di tema (sovrascritti dal tema chiaro "Perla") */
  --fg-rgb: 255, 255, 255;          /* colore delle sovrapposizioni traslucide */
  --bg-rgb: 10, 11, 16;
  --glass: rgba(22, 24, 34, 0.55);
  --glass-focus: rgba(24, 26, 38, 0.7);
  --sidebar-glass: rgba(11, 12, 18, 0.72);
  --modal-bg: rgba(16, 17, 25, 0.88);
  --toast-bg: rgba(26, 30, 40, 0.92);
  --bubble: linear-gradient(180deg, rgba(42, 44, 62, 0.9), rgba(30, 32, 46, 0.9));
  --backdrop: rgba(3, 4, 8, 0.6);
  --text-strong: #ffffff;
  --code-inline: #f0abfc;
  --danger-text: #fca5a5;
  --danger-text-2: #fecaca;
  --success-text: #a7f3d0;
  --warning-text: #fde68a;
  --stop-bg: #f4f3fb;
  --stop-fg: #15131f;
  --aurora-opacity: 1;

  color-scheme: dark;
}

/* ---------- Tema chiaro "Perla" ---------- */
:root[data-theme="light"] {
  --bg: #f6f5fb;
  --bg-2: #fbfaff;
  --sidebar: #f1eff8;
  --panel: #ffffff;
  --panel-2: #f5f3fb;
  --panel-3: #ece9f6;
  --hover: rgba(46, 34, 92, 0.05);
  --active: rgba(46, 34, 92, 0.085);
  --border: rgba(46, 34, 92, 0.09);
  --border-strong: rgba(46, 34, 92, 0.14);

  --text: #1c1930;
  --text-2: #4b4766;
  --muted: #8a85a3;

  --danger: #e5484d;
  --danger-rgb: 229, 72, 77;
  --success: #12a87a;
  --warning: #d48a06;

  --shadow: 0 18px 50px -14px rgba(76, 52, 160, 0.22);
  --shadow-sm: 0 4px 16px -6px rgba(76, 52, 160, 0.16);

  --fg-rgb: 46, 34, 92;
  --bg-rgb: 246, 245, 251;
  --glass: rgba(255, 255, 255, 0.62);
  --glass-focus: rgba(255, 255, 255, 0.82);
  --sidebar-glass: rgba(244, 242, 251, 0.75);
  --modal-bg: rgba(252, 251, 255, 0.9);
  --toast-bg: rgba(255, 255, 255, 0.92);
  --bubble: linear-gradient(180deg, #ffffff, #f3f0fc);
  --backdrop: rgba(60, 48, 110, 0.22);
  --text-strong: #110e22;
  --code-inline: #9333ea;
  --danger-text: #c53035;
  --danger-text-2: #b42328;
  --success-text: #0b7a58;
  --warning-text: #8a5a00;
  --stop-bg: #1c1930;
  --stop-fg: #ffffff;
  --aurora-opacity: 1.7;

  color-scheme: light;
}

/* I blocchi di codice restano scuri anche nel tema chiaro */
:root[data-theme="light"] .code-block {
  --fg-rgb: 255, 255, 255;
  --text: #eceef4;
  --muted: #8b93a7;
  --border: rgba(255, 255, 255, 0.08);
  border-color: rgba(46, 34, 92, 0.12);
}

/* Transizione morbida quando si cambia tema */
html.theme-switching *,
html.theme-switching *::before,
html.theme-switching *::after {
  transition: background-color 0.45s var(--ease), border-color 0.45s var(--ease), color 0.45s var(--ease), fill 0.45s !important;
}

/* ---------- Base ---------- */

*,
*::before,
*::after { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.6;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}

button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; padding: 0; }
button:disabled { cursor: not-allowed; }
[hidden] { display: none !important; }
.ic { flex-shrink: 0; display: block; }

:focus-visible {
  outline: 2px solid rgba(var(--accent-rgb), 0.7);
  outline-offset: 2px;
}

::selection { background: rgba(var(--accent-rgb), 0.35); }

/* Scrollbar sottili */
* { scrollbar-width: thin; scrollbar-color: rgba(var(--fg-rgb), 0.12) transparent; }
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: rgba(var(--fg-rgb), 0.1); border-radius: 10px; border: 3px solid transparent; background-clip: padding-box; }
::-webkit-scrollbar-thumb:hover { background-color: rgba(var(--fg-rgb), 0.18); }
::-webkit-scrollbar-track { background: transparent; }

/* Bagliore di sfondo */
.bg-glow {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 0;
  background:
    radial-gradient(600px 400px at 85% -10%, rgba(var(--accent-rgb), 0.10), transparent 70%),
    radial-gradient(500px 380px at 10% 110%, rgba(34, 211, 238, 0.05), transparent 70%);
  transition: background 0.6s var(--ease);
}

/* ---------- Layout ---------- */

.app {
  position: relative;
  z-index: 1;
  display: grid;
  grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
  height: 100vh;
  height: 100dvh;
  transition: grid-template-columns 0.38s var(--ease);
}

.app.sidebar-collapsed { grid-template-columns: 0 minmax(0, 1fr); }

/* ---------- Logo ---------- */

.logo {
  --size: 30px;
  width: var(--size);
  height: var(--size);
  border-radius: calc(var(--size) * 0.3);
  background: var(--grad);
  display: grid;
  place-items: center;
  flex-shrink: 0;
  overflow: hidden;
  color: #fff;
  font-weight: 700;
  font-size: calc(var(--size) * 0.46);
  letter-spacing: -0.02em;
  box-shadow: 0 4px 14px -4px rgba(var(--accent-rgb), 0.6), inset 0 1px 0 rgba(255, 255, 255, 0.25);
  user-select: none;
}
.logo img { width: 100%; height: 100%; object-fit: cover; }
.logo span { line-height: 1; text-shadow: 0 1px 2px rgba(0, 0, 0, 0.2); }
.logo-lg { --size: 64px; }
.logo-xl { --size: 76px; }

/* ---------- Pulsanti generici ---------- */

.icon-btn {
  width: 36px;
  height: 36px;
  display: inline-grid;
  place-items: center;
  border-radius: var(--radius-sm);
  color: var(--muted);
  transition: background 0.18s, color 0.18s, transform 0.18s;
  flex-shrink: 0;
}
.icon-btn:hover { background: var(--hover); color: var(--text); }
.icon-btn:active { transform: scale(0.94); }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  height: 38px;
  padding: 0 16px;
  border-radius: var(--radius-sm);
  background: var(--panel-2);
  border: 1px solid var(--border-strong);
  color: var(--text);
  font-weight: 500;
  font-size: 14px;
  white-space: nowrap;
  transition: background 0.18s, border-color 0.18s, transform 0.15s, box-shadow 0.2s, opacity 0.2s;
}
.btn:hover { background: var(--panel-3); }
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.55; }
.btn-sm { height: 32px; padding: 0 12px; font-size: 13px; }
.btn-icon { width: 38px; padding: 0; }
.btn-ghost { background: transparent; border-color: transparent; color: var(--text-2); }
.btn-ghost:hover { background: var(--hover); color: var(--text); }
.btn-primary {
  background: var(--grad);
  border-color: transparent;
  color: #fff;
  box-shadow: 0 6px 20px -8px rgba(var(--accent-rgb), 0.8);
}
.btn-primary:hover { background: var(--grad); filter: brightness(1.08); box-shadow: 0 8px 24px -8px rgba(var(--accent-rgb), 0.95); }
.btn-danger {
  background: rgba(var(--danger-rgb), 0.12);
  border-color: rgba(var(--danger-rgb), 0.35);
  color: var(--danger-text);
}
.btn-danger:hover { background: rgba(var(--danger-rgb), 0.2); }

.link-btn {
  color: var(--accent-2);
  font-weight: 500;
  font-size: inherit;
  text-decoration: underline;
  text-decoration-color: transparent;
  text-underline-offset: 3px;
  transition: text-decoration-color 0.2s;
}
.link-btn:hover { text-decoration-color: currentColor; }

kbd {
  font-family: var(--font);
  font-size: 11px;
  color: var(--muted);
  background: rgba(var(--fg-rgb), 0.05);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 1px 6px;
}

/* =========================================================
   BARRA LATERALE
   ========================================================= */

.sidebar {
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow: hidden;
  background: var(--sidebar);
  border-right: 1px solid var(--border);
  padding: 12px 10px calc(10px + env(safe-area-inset-bottom));
}
.sidebar > * { min-width: calc(var(--sidebar-w) - 20px); }
.app.sidebar-collapsed .sidebar { border-right-color: transparent; }

.sidebar-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 2px 4px 12px 6px;
}

.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand-name {
  font-weight: 650;
  font-size: 16px;
  letter-spacing: -0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.new-chat {
  display: flex;
  align-items: center;
  gap: 10px;
  height: 42px;
  padding: 0 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: linear-gradient(180deg, rgba(var(--fg-rgb), 0.04), rgba(var(--fg-rgb), 0.01));
  font-weight: 550;
  font-size: 14px;
  transition: border-color 0.2s, background 0.2s, transform 0.15s, box-shadow 0.25s;
}
.new-chat:hover {
  border-color: rgba(var(--accent-rgb), 0.5);
  background: rgba(var(--accent-rgb), 0.08);
  box-shadow: 0 0 0 4px rgba(var(--accent-rgb), 0.08);
}
.new-chat:active { transform: scale(0.98); }
.new-chat .ic { color: var(--accent); transition: transform 0.35s var(--ease-spring); }
.new-chat:hover .ic { transform: rotate(90deg); }
.new-chat kbd { margin-left: auto; }

.search {
  position: relative;
  display: flex;
  align-items: center;
  margin: 10px 0 6px;
}
.search > span { position: absolute; left: 11px; color: var(--muted); pointer-events: none; }
.search input {
  width: 100%;
  height: 36px;
  padding: 0 12px 0 34px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  background: rgba(var(--fg-rgb), 0.035);
  font-size: 13.5px;
  outline: none;
  transition: border-color 0.2s, background 0.2s;
}
.search input::placeholder { color: var(--muted); }
.search input:focus { border-color: rgba(var(--accent-rgb), 0.45); background: rgba(var(--fg-rgb), 0.05); }
.search input::-webkit-search-cancel-button { filter: invert(0.6); cursor: pointer; }

.conv-list {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  margin: 4px -4px;
  padding: 0 4px;
}

.conv-group { margin-top: 14px; }
.conv-group-label {
  font-size: 11.5px;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  padding: 0 10px 6px;
}

.conv-item {
  position: relative;
  display: flex;
  align-items: center;
  height: 38px;
  padding: 0 10px;
  border-radius: var(--radius-sm);
  color: var(--text-2);
  font-size: 14px;
  cursor: pointer;
  outline: none;
  transition: background 0.15s, color 0.15s;
  animation: fadeSlideIn 0.35s var(--ease) both;
}
.conv-item:hover, .conv-item:focus-visible { background: var(--hover); color: var(--text); }
.conv-item.active { background: var(--active); color: var(--text); }
.conv-item.active::before {
  content: "";
  position: absolute;
  left: 0;
  top: 9px;
  bottom: 9px;
  width: 3px;
  border-radius: 3px;
  background: var(--grad);
}

.conv-title {
  flex: 1;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  -webkit-mask-image: linear-gradient(90deg, #000 85%, transparent);
  mask-image: linear-gradient(90deg, #000 85%, transparent);
}

.conv-actions {
  display: flex;
  gap: 2px;
  margin-left: 4px;
  opacity: 0;
  transform: translateX(6px);
  transition: opacity 0.18s, transform 0.18s var(--ease);
}
.conv-item:hover .conv-actions,
.conv-item.active .conv-actions,
.conv-item:focus-within .conv-actions { opacity: 1; transform: none; }

.conv-btn {
  width: 26px;
  height: 26px;
  display: grid;
  place-items: center;
  border-radius: 7px;
  color: var(--muted);
  transition: background 0.15s, color 0.15s;
}
.conv-btn:hover { background: rgba(var(--fg-rgb), 0.08); color: var(--text); }
.conv-btn.danger:hover { background: rgba(var(--danger-rgb), 0.15); color: var(--danger); }

.conv-rename {
  flex: 1;
  min-width: 0;
  height: 28px;
  padding: 0 8px;
  border-radius: 7px;
  border: 1px solid rgba(var(--accent-rgb), 0.6);
  background: var(--bg);
  outline: none;
  font-size: 14px;
  box-shadow: 0 0 0 3px rgba(var(--accent-rgb), 0.15);
}
.conv-item.renaming .conv-actions { display: none; }

.conv-empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 40px 16px;
  color: var(--muted);
  text-align: center;
  font-size: 13.5px;
}
.conv-empty p { margin: 0; }
.conv-empty .ic { opacity: 0.6; }

.sidebar-bottom {
  border-top: 1px solid var(--border);
  padding-top: 8px;
  margin-top: 4px;
}
.side-btn {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  height: 40px;
  padding: 0 10px;
  border-radius: var(--radius-sm);
  color: var(--text-2);
  font-size: 14px;
  transition: background 0.15s, color 0.15s;
}
.side-btn:hover { background: var(--hover); color: var(--text); }
.side-btn .ic { transition: transform 0.6s var(--ease); }
.side-btn:hover .ic { transform: rotate(90deg); }

.overlay { display: none; }

/* =========================================================
   AREA PRINCIPALE
   ========================================================= */

.main {
  position: relative;
  display: flex;
  flex-direction: column;
  min-width: 0;
  height: 100vh;
  height: 100dvh;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 6px;
  height: 56px;
  padding: 0 14px;
  padding-top: env(safe-area-inset-top);
  box-sizing: content-box;
  flex-shrink: 0;
  position: relative;
  z-index: 3;
  background: linear-gradient(180deg, var(--bg) 40%, rgba(var(--bg-rgb), 0));
}

.btn-menu, .btn-new-top { display: none; }
.app.sidebar-collapsed .btn-menu,
.app.sidebar-collapsed .btn-new-top { display: inline-grid; animation: fadeIn 0.3s var(--ease); }

.topbar-title {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 10px;
  padding-left: 6px;
}
.chat-title {
  font-weight: 600;
  font-size: 15px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.model-tag {
  flex-shrink: 0;
  font-size: 12px;
  font-family: var(--mono);
  color: var(--muted);
  background: rgba(var(--fg-rgb), 0.04);
  border: 1px solid var(--border);
  padding: 1px 8px;
  border-radius: 999px;
}

/* Indicatore di stato */
.status {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  height: 32px;
  padding: 0 12px 0 10px;
  border-radius: 999px;
  border: 1px solid var(--border-strong);
  background: rgba(var(--fg-rgb), 0.03);
  font-size: 12.5px;
  font-weight: 550;
  color: var(--text-2);
  white-space: nowrap;
  flex-shrink: 0;
  transition: background 0.25s, border-color 0.25s, color 0.25s;
}
.status:hover { background: rgba(var(--fg-rgb), 0.06); }
.status-dot {
  --c: var(--accent);
  position: relative;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--c);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--c) 22%, transparent);
}
.status-dot::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: 50%;
  background: var(--c);
  animation: ping 2s cubic-bezier(0, 0, 0.2, 1) infinite;
}
.status-demo .status-dot { --c: var(--warning); }
.status-demo { border-color: rgba(251, 191, 36, 0.25); }
.status-online .status-dot { --c: var(--success); }
.status-online { border-color: rgba(52, 211, 153, 0.3); color: var(--success-text); }
.status-offline .status-dot { --c: var(--danger); }
.status-offline .status-dot::after { animation: none; }
.status-offline { border-color: rgba(var(--danger-rgb), 0.35); color: var(--danger-text-2); }
.status-checking .status-dot { --c: var(--muted); }
.status-checking .status-dot::after { animation-duration: 0.9s; }

/* ---------- Chat ---------- */

.chat {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  scroll-behavior: auto;
  overscroll-behavior: contain;
  position: relative;
  margin-top: -14px;
  padding-top: 14px;
}

/* margin:auto sul contenuto centra senza tagliarlo quando lo schermo è basso */
.chat.is-empty {
  display: flex;
  flex-direction: column;
}

.messages {
  max-width: var(--content-w);
  margin: 0 auto;
  padding: 12px 24px 40px;
}

/* ---------- Schermata di benvenuto ---------- */

.welcome {
  width: 100%;
  max-width: var(--content-w);
  padding: 24px;
  margin: auto;
  text-align: center;
  animation: fadeSlideUp 0.7s var(--ease) both;
}

.welcome h1 {
  margin: 0;
  font-size: clamp(26px, 4.2vw, 36px);
  font-weight: 700;
  letter-spacing: -0.025em;
  line-height: 1.2;
}
.grad-text {
  background: var(--grad);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.welcome-sub {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 17px;
}

.demo-banner {
  display: inline-flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 8px;
  margin-top: 22px;
  padding: 8px 14px;
  border-radius: 999px;
  border: 1px solid rgba(251, 191, 36, 0.25);
  background: rgba(251, 191, 36, 0.06);
  color: var(--warning-text);
  font-size: 13px;
}

.suggestions {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  margin-top: 30px;
  text-align: left;
}
.suggestion {
  display: grid;
  grid-template-columns: auto 1fr;
  grid-template-rows: auto auto;
  column-gap: 12px;
  align-items: center;
  text-align: left;
  padding: 14px 16px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: rgba(var(--fg-rgb), 0.025);
  transition: border-color 0.25s, background 0.25s, transform 0.25s var(--ease), box-shadow 0.3s;
  animation: fadeSlideUp 0.6s var(--ease) both;
}
.suggestion:nth-child(1) { animation-delay: 0.08s; }
.suggestion:nth-child(2) { animation-delay: 0.14s; }
.suggestion:nth-child(3) { animation-delay: 0.2s; }
.suggestion:nth-child(4) { animation-delay: 0.26s; }
.suggestion:hover {
  border-color: rgba(var(--accent-rgb), 0.4);
  background: rgba(var(--accent-rgb), 0.06);
  transform: translateY(-2px);
  box-shadow: 0 10px 30px -14px rgba(var(--accent-rgb), 0.6);
}
.suggestion:active { transform: translateY(0) scale(0.99); }
.sg-icon {
  grid-row: span 2;
  width: 36px;
  height: 36px;
  display: grid;
  place-items: center;
  border-radius: 10px;
  background: rgba(var(--accent-rgb), 0.12);
  color: var(--accent);
}
.sg-title, .sg-text { grid-column: 2; min-width: 0; }
.sg-title { font-weight: 600; font-size: 14px; }
.sg-text { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---------- Messaggi ---------- */

.msg { position: relative; margin: 0 0 26px; }
.msg-enter { animation: fadeSlideUp 0.45s var(--ease) both; }

/* Utente */
.msg-user {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
}
.msg-bubble {
  max-width: min(85%, 620px);
  padding: 11px 16px;
  border-radius: var(--radius-lg) var(--radius-lg) 6px var(--radius-lg);
  background: linear-gradient(180deg, var(--panel-3), var(--panel-2));
  border: 1px solid var(--border);
  white-space: pre-wrap;
  word-wrap: break-word;
  overflow-wrap: anywhere;
  box-shadow: var(--shadow-sm);
}
.msg-user .msg-actions { margin-top: 4px; opacity: 0; transition: opacity 0.2s; }
.msg-user:hover .msg-actions { opacity: 1; }

/* Assistente */
.msg-assistant {
  display: grid;
  grid-template-columns: 30px minmax(0, 1fr);
  gap: 14px;
}
.msg-assistant .avatar { --size: 30px; margin-top: 1px; }
.msg-assistant.streaming .avatar { animation: glowPulse 1.6s ease-in-out infinite; }
.msg-name { font-weight: 600; font-size: 14px; margin-bottom: 2px; color: var(--text); }
.msg-body { min-width: 0; }

.msg-actions {
  display: flex;
  align-items: center;
  gap: 2px;
  margin-top: 8px;
  animation: fadeIn 0.3s var(--ease) both;
}
.act {
  width: 30px;
  height: 30px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  color: var(--muted);
  transition: background 0.15s, color 0.15s, transform 0.15s;
}
.act:hover { background: var(--hover); color: var(--text); }
.act:active { transform: scale(0.9); }
.act.copied { color: var(--success); }
.msg-meta {
  display: inline-flex;
  gap: 8px;
  margin-left: 8px;
  font-size: 12px;
  color: var(--muted);
}
.meta-model { font-family: var(--mono); opacity: 0.85; }
.msg-meta span + span::before { content: "·"; margin-right: 8px; }

.msg-error {
  display: flex;
  gap: 10px;
  margin-top: 10px;
  padding: 12px 14px;
  border-radius: var(--radius);
  border: 1px solid rgba(var(--danger-rgb), 0.3);
  background: rgba(var(--danger-rgb), 0.07);
  color: var(--danger-text-2);
  font-size: 14px;
  animation: shake 0.4s var(--ease);
}
.msg-error .ic { color: var(--danger); margin-top: 3px; }
.msg-error p { margin: 2px 0 6px; color: var(--danger-text); word-break: break-word; }

.msg-note {
  display: inline-block;
  margin-top: 8px;
  font-size: 12.5px;
  color: var(--muted);
  font-style: italic;
}

/* Indicatore di scrittura */
.typing { display: flex; gap: 5px; padding: 10px 0 4px; }
.typing span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--grad);
  animation: bounce 1.2s infinite ease-in-out;
}
.typing span:nth-child(2) { animation-delay: 0.15s; }
.typing span:nth-child(3) { animation-delay: 0.3s; }

/* Cursore lampeggiante durante lo streaming */
.cursor {
  display: inline-block;
  width: 8px;
  height: 1.05em;
  margin-left: 2px;
  vertical-align: -0.15em;
  border-radius: 2px;
  background: var(--grad);
  animation: blink 1s steps(2, start) infinite;
}

/* Ragionamento (qwen3 <think>) */
.thinking {
  margin: 4px 0 10px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: rgba(var(--fg-rgb), 0.02);
  overflow: hidden;
  animation: fadeIn 0.3s var(--ease);
}
.thinking summary {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  cursor: pointer;
  list-style: none;
  color: var(--muted);
  font-size: 13px;
  font-weight: 550;
  user-select: none;
  transition: color 0.2s;
}
.thinking summary::-webkit-details-marker { display: none; }
.thinking summary:hover { color: var(--text-2); }
.thinking summary .ic:last-child { margin-left: auto; transition: transform 0.25s var(--ease); }
.thinking[open] summary .ic:last-child { transform: rotate(90deg); }
.thinking.active .thinking-label {
  background: linear-gradient(90deg, var(--muted) 0%, var(--text) 50%, var(--muted) 100%);
  background-size: 200% 100%;
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  animation: shimmer 1.6s linear infinite;
}
.thinking.active summary .ic:first-child { color: var(--accent); animation: pulse 1.4s ease-in-out infinite; }
.thinking-body {
  padding: 2px 14px 12px 34px;
  font-size: 13.5px;
  color: var(--muted);
  border-top: 1px solid var(--border);
}
.thinking-body > :first-child { margin-top: 10px; }

/* =========================================================
   MARKDOWN
   ========================================================= */

.markdown { overflow-wrap: anywhere; }
.markdown > :first-child { margin-top: 0; }
.markdown > :last-child { margin-bottom: 0; }
.markdown p { margin: 0 0 0.85em; }
.markdown h1, .markdown h2, .markdown h3, .markdown h4, .markdown h5, .markdown h6 {
  margin: 1.4em 0 0.55em;
  line-height: 1.3;
  font-weight: 650;
  letter-spacing: -0.015em;
}
.markdown h1 { font-size: 1.55em; }
.markdown h2 { font-size: 1.3em; }
.markdown h3 { font-size: 1.12em; }
.markdown h4, .markdown h5, .markdown h6 { font-size: 1em; }
.markdown strong { font-weight: 650; color: var(--text-strong); }
.markdown em { color: var(--text); }
.markdown del { color: var(--muted); }
.markdown a {
  color: var(--accent-2);
  text-decoration: underline;
  text-decoration-color: rgba(34, 211, 238, 0.35);
  text-underline-offset: 3px;
  transition: text-decoration-color 0.2s;
}
.markdown a:hover { text-decoration-color: currentColor; }
.markdown ul, .markdown ol { margin: 0 0 0.85em; padding-left: 1.6em; }
.markdown li { margin: 0.25em 0; }
.markdown li > ul, .markdown li > ol { margin: 0.25em 0 0.25em; }
.markdown ul li::marker { color: var(--accent); }
.markdown ol li::marker { color: var(--muted); font-weight: 600; }
.markdown li.task-item { list-style: none; margin-left: -1.3em; }
.markdown .task {
  display: inline-block;
  width: 15px;
  height: 15px;
  margin-right: 8px;
  vertical-align: -2px;
  border-radius: 4px;
  border: 1.5px solid var(--border-strong);
}
.markdown .task.done {
  border-color: transparent;
  background: var(--grad) center / 100% no-repeat;
  -webkit-mask: none;
  position: relative;
}
.markdown .task.done::after {
  content: "";
  position: absolute;
  left: 4.5px;
  top: 1.5px;
  width: 4px;
  height: 8px;
  border: solid #fff;
  border-width: 0 2px 2px 0;
  transform: rotate(45deg);
}
.markdown blockquote {
  margin: 0 0 0.85em;
  padding: 8px 16px;
  border-left: 3px solid var(--accent);
  background: rgba(var(--accent-rgb), 0.06);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  color: var(--text-2);
}
.markdown hr {
  border: 0;
  height: 1px;
  margin: 1.6em 0;
  background: linear-gradient(90deg, transparent, var(--border-strong), transparent);
}
.markdown code {
  font-family: var(--mono);
  font-size: 0.87em;
  padding: 0.15em 0.4em;
  border-radius: 6px;
  background: rgba(var(--fg-rgb), 0.07);
  border: 1px solid var(--border);
  color: var(--code-inline);
}

/* Tabelle */
.table-wrap {
  margin: 0 0 1em;
  overflow-x: auto;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
}
.markdown table { width: 100%; border-collapse: collapse; font-size: 14px; }
.markdown th, .markdown td { padding: 9px 14px; text-align: left; border-bottom: 1px solid var(--border); }
.markdown th { background: rgba(var(--fg-rgb), 0.04); font-weight: 600; white-space: nowrap; }
.markdown tr:last-child td { border-bottom: 0; }
.markdown tbody tr { transition: background 0.15s; }
.markdown tbody tr:hover { background: rgba(var(--fg-rgb), 0.025); }

/* Blocchi di codice */
.code-block {
  margin: 0 0 1em;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: #0b0d13;
  overflow: hidden;
  box-shadow: var(--shadow-sm);
}
.code-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 38px;
  padding: 0 8px 0 14px;
  background: rgba(var(--fg-rgb), 0.035);
  border-bottom: 1px solid var(--border);
}
.code-lang { font-family: var(--mono); font-size: 12px; color: var(--muted); text-transform: lowercase; }
.copy-code {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 28px;
  padding: 0 10px;
  border-radius: 7px;
  color: var(--muted);
  font-size: 12.5px;
  font-weight: 500;
  transition: background 0.15s, color 0.15s;
}
.copy-code:hover { background: rgba(var(--fg-rgb), 0.07); color: var(--text); }
.copy-code.copied { color: var(--success); }
.code-block pre {
  margin: 0;
  padding: 14px 16px;
  overflow-x: auto;
  font-family: var(--mono);
  font-size: 13.5px;
  line-height: 1.6;
  tab-size: 4;
}
.code-block pre code {
  font: inherit;
  background: none;
  border: 0;
  padding: 0;
  color: #d6dae4;
  white-space: pre;
}
.tk-k { color: #c792ea; }
.tk-s { color: #a5e3a0; }
.tk-n { color: #f78c6c; }
.tk-c { color: #636b7d; font-style: italic; }
.tk-f { color: #82aaff; }

/* ---------- Pulsante "vai in fondo" ---------- */

.scroll-bottom {
  position: absolute;
  left: 50%;
  bottom: 118px;
  z-index: 4;
  width: 36px;
  height: 36px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  border: 1px solid var(--border-strong);
  background: var(--panel-2);
  color: var(--text);
  box-shadow: var(--shadow);
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 10px) scale(0.9);
  transition: opacity 0.25s, transform 0.3s var(--ease);
}
.scroll-bottom.visible { opacity: 1; pointer-events: auto; transform: translate(-50%, 0) scale(1); }
.scroll-bottom:hover { background: var(--panel-3); }

/* ---------- Composer ---------- */

.composer-wrap {
  flex-shrink: 0;
  padding: 0 24px calc(12px + env(safe-area-inset-bottom));
  position: relative;
  z-index: 3;
}
.composer-wrap::before {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  bottom: 100%;
  height: 30px;
  background: linear-gradient(0deg, var(--bg), transparent);
  pointer-events: none;
}

.composer {
  position: relative;
  display: flex;
  align-items: flex-end;
  gap: 10px;
  max-width: var(--content-w);
  margin: 0 auto;
  padding: 8px 8px 8px 18px;
  border-radius: var(--radius-xl);
  border: 1px solid var(--border-strong);
  background: var(--panel);
  box-shadow: var(--shadow);
  transition: border-color 0.25s, box-shadow 0.3s;
}
.composer:focus-within {
  border-color: rgba(var(--accent-rgb), 0.5);
  box-shadow: var(--shadow), 0 0 0 4px rgba(var(--accent-rgb), 0.1);
}
.composer.generating::after {
  content: "";
  position: absolute;
  inset: -1px;
  border-radius: inherit;
  padding: 1px;
  background: linear-gradient(90deg, transparent, var(--accent), var(--accent-2), transparent);
  background-size: 200% 100%;
  -webkit-mask: linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0);
  -webkit-mask-composite: xor;
  mask: linear-gradient(#000 0 0) content-box exclude, linear-gradient(#000 0 0);
  animation: shimmer 2s linear infinite;
  pointer-events: none;
}

.composer textarea {
  flex: 1;
  min-height: 40px;
  max-height: 220px;
  padding: 8px 0;
  border: 0;
  outline: none;
  resize: none;
  background: transparent;
  font-size: 15px;
  line-height: 1.55;
  overflow-y: auto;
}
.composer textarea::placeholder { color: var(--muted); }

.send-btn {
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  flex-shrink: 0;
  border-radius: 50%;
  background: var(--grad);
  color: #fff;
  box-shadow: 0 6px 18px -6px rgba(var(--accent-rgb), 0.8);
  transition: transform 0.2s var(--ease-spring), opacity 0.2s, filter 0.2s, box-shadow 0.25s, background 0.25s;
}
.send-btn:hover:not(:disabled) { transform: scale(1.06); filter: brightness(1.1); }
.send-btn:active:not(:disabled) { transform: scale(0.92); }
.send-btn:disabled { opacity: 0.3; box-shadow: none; filter: grayscale(0.6); }
.send-btn .ic { animation: popIn 0.25s var(--ease-spring); }
.send-btn.is-stop { background: var(--text); color: var(--bg); box-shadow: 0 0 0 4px rgba(var(--fg-rgb), 0.08); }

.disclaimer {
  margin: 8px 0 0;
  text-align: center;
  font-size: 12px;
  color: var(--muted);
}

/* =========================================================
   MODALI
   ========================================================= */

.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.modal.open { display: flex; }
.modal-backdrop {
  position: absolute;
  inset: 0;
  background: var(--backdrop);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
  animation: fadeIn 0.22s var(--ease) both;
}
.modal-panel {
  position: relative;
  display: flex;
  flex-direction: column;
  width: 100%;
  max-height: min(88dvh, 760px);
  border-radius: var(--radius-lg);
  border: 1px solid var(--border-strong);
  background: var(--bg-2);
  box-shadow: 0 30px 80px -20px rgba(0, 0, 0, 0.8), 0 0 0 1px rgba(var(--fg-rgb), 0.02) inset;
  animation: modalIn 0.32s var(--ease) both;
}
.modal-lg { max-width: 620px; }
.modal-sm { max-width: 420px; }
.modal.closing .modal-backdrop { animation: fadeOut 0.18s ease both; }
.modal.closing .modal-panel { animation: modalOut 0.18s ease both; }

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 16px 8px 22px;
}
.modal-header h2 { margin: 0; font-size: 18px; font-weight: 650; letter-spacing: -0.01em; }

.tabs {
  display: flex;
  gap: 4px;
  padding: 6px 16px 0;
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
  scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }
.tab {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 10px 12px 12px;
  color: var(--muted);
  font-size: 14px;
  font-weight: 550;
  white-space: nowrap;
  transition: color 0.2s;
}
.tab:hover { color: var(--text-2); }
.tab.active { color: var(--text); }
.tab::after {
  content: "";
  position: absolute;
  left: 10px;
  right: 10px;
  bottom: -1px;
  height: 2px;
  border-radius: 2px;
  background: var(--grad);
  transform: scaleX(0);
  transition: transform 0.3s var(--ease);
}
.tab.active::after { transform: scaleX(1); }
.tab.active .ic { color: var(--accent); }

.modal-body { flex: 1; overflow-y: auto; padding: 20px 22px; }

.tab-panel { display: none; }
.tab-panel.active { display: block; animation: fadeSlideUp 0.3s var(--ease) both; }

.modal-footer {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 14px 16px;
  border-top: 1px solid var(--border);
}
.spacer { flex: 1; }

.confirm-body { padding: 22px 22px 8px; }
.confirm-title { margin: 0 0 8px; font-size: 17px; font-weight: 650; }
.confirm-text { margin: 0; color: var(--text-2); font-size: 14px; }
#confirmModal .modal-footer { border-top: 0; }

/* ---------- Campi ---------- */

.field { margin-bottom: 20px; }
.field label, .label-row label { display: block; margin-bottom: 7px; font-size: 13.5px; font-weight: 600; color: var(--text-2); }
.label-row { display: flex; align-items: center; justify-content: space-between; }
.label-row label { margin-bottom: 7px; }
.field input[type="text"],
.field input[type="url"],
.field textarea {
  width: 100%;
  padding: 10px 13px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--panel);
  outline: none;
  font-size: 14px;
  transition: border-color 0.2s, box-shadow 0.2s, background 0.2s;
}
.field input[type="text"],
.field input[type="url"] { height: 40px; }
.field textarea { resize: vertical; min-height: 120px; line-height: 1.55; }
.field input:focus,
.field textarea:focus {
  border-color: rgba(var(--accent-rgb), 0.6);
  box-shadow: 0 0 0 3px rgba(var(--accent-rgb), 0.14);
  background: var(--panel-2);
}
.field input::placeholder, .field textarea::placeholder { color: var(--muted); }

.input-row { display: flex; gap: 8px; }
.input-row input { flex: 1; min-width: 0; }

.field-grid { display: grid; grid-template-columns: 2fr 1fr; gap: 12px; }

.hint { margin: 7px 0 0; font-size: 12.5px; color: var(--muted); line-height: 1.5; }
.hint:empty { display: none; }
.hint.ok { color: var(--success); }
.hint.err { color: var(--danger-text); }
.hint code {
  font-family: var(--mono);
  font-size: 0.92em;
  padding: 1px 5px;
  border-radius: 5px;
  background: rgba(var(--fg-rgb), 0.06);
  color: var(--text-2);
}

.fields { transition: opacity 0.3s; }
.fields.dimmed { opacity: 0.5; }

.value-badge {
  font-family: var(--mono);
  font-size: 12.5px;
  padding: 2px 8px;
  border-radius: 6px;
  background: rgba(var(--accent-rgb), 0.14);
  color: var(--accent);
  margin-bottom: 7px;
}

/* Slider */
input[type="range"] {
  width: 100%;
  height: 22px;
  -webkit-appearance: none;
  appearance: none;
  background: transparent;
  cursor: pointer;
}
input[type="range"]::-webkit-slider-runnable-track {
  height: 6px;
  border-radius: 6px;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  opacity: 0.9;
}
input[type="range"]::-moz-range-track { height: 6px; border-radius: 6px; background: linear-gradient(90deg, var(--accent), var(--accent-2)); }
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 18px;
  height: 18px;
  margin-top: -6px;
  border-radius: 50%;
  background: #fff;
  border: 0;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.5), 0 0 0 4px rgba(var(--accent-rgb), 0.25);
  transition: transform 0.15s;
}
input[type="range"]::-moz-range-thumb {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #fff;
  border: 0;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.5), 0 0 0 4px rgba(var(--accent-rgb), 0.25);
}
input[type="range"]:active::-webkit-slider-thumb { transform: scale(1.15); }
.range-labels { display: flex; justify-content: space-between; font-size: 12px; color: var(--muted); margin-top: 4px; }

/* Interruttore */
.switch { position: relative; display: inline-flex; cursor: pointer; flex-shrink: 0; }
.switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.switch-track {
  width: 46px;
  height: 26px;
  border-radius: 999px;
  background: var(--panel-3);
  border: 1px solid var(--border-strong);
  position: relative;
  transition: background 0.25s, border-color 0.25s;
}
.switch-thumb {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.4);
  transition: transform 0.3s var(--ease-spring);
}
.switch input:checked + .switch-track { background: var(--grad); border-color: transparent; }
.switch input:checked + .switch-track .switch-thumb { transform: translateX(20px); }
.switch input:focus-visible + .switch-track { outline: 2px solid rgba(var(--accent-rgb), 0.7); outline-offset: 2px; }

/* Card impostazioni */
.setting-card {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 16px 18px;
  margin-bottom: 22px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: rgba(var(--fg-rgb), 0.02);
  transition: border-color 0.3s, background 0.3s;
}
.setting-card-text { flex: 1; min-width: 0; }
.setting-card h3 { margin: 0 0 3px; font-size: 15px; font-weight: 600; }
.setting-card p { margin: 0; font-size: 13px; color: var(--muted); }
.demo-card.on { border-color: rgba(251, 191, 36, 0.3); background: rgba(251, 191, 36, 0.05); }

/* Chip modelli */
.model-chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.model-chips:empty { display: none; }
.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 30px;
  padding: 0 12px;
  border-radius: 999px;
  border: 1px solid var(--border-strong);
  background: var(--panel);
  font-family: var(--mono);
  font-size: 12.5px;
  animation: popIn 0.3s var(--ease-spring) both;
  transition: border-color 0.2s, background 0.2s;
}
.chip small { color: var(--muted); font-size: 11px; }
.chip:hover { border-color: rgba(var(--accent-rgb), 0.5); }
.chip.active { border-color: var(--accent); background: rgba(var(--accent-rgb), 0.14); }

/* Editor logo */
.logo-editor {
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 18px;
  margin-bottom: 22px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: radial-gradient(240px 120px at 0% 50%, rgba(var(--accent-rgb), 0.1), transparent), rgba(var(--fg-rgb), 0.02);
}
.logo-editor .logo { border-radius: 18px; }
.logo-preview-name { font-size: 20px; font-weight: 700; letter-spacing: -0.02em; margin-bottom: 8px; }
.logo-buttons { display: flex; flex-wrap: wrap; gap: 6px; }

/* Colori */
.swatches { display: flex; flex-wrap: wrap; gap: 10px; }
.swatch {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  padding: 3px;
  border: 2px solid transparent;
  transition: border-color 0.2s, transform 0.2s var(--ease-spring);
}
.swatch span {
  display: block;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--sw-a), var(--sw-b));
}
.swatch:hover { transform: scale(1.1); }
.swatch.active { border-color: var(--text); }

/* Dati */
.stats-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.stat {
  display: flex;
  flex-direction: column;
  padding: 14px 16px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: rgba(var(--fg-rgb), 0.02);
}
.stat-value { font-size: 22px; font-weight: 700; letter-spacing: -0.02em; }
.stat-label { font-size: 12.5px; color: var(--muted); }
.data-actions { display: flex; flex-wrap: wrap; gap: 8px; margin: 18px 0 24px; }
.danger-zone {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 16px 18px;
  border-radius: var(--radius);
  border: 1px solid rgba(var(--danger-rgb), 0.25);
  background: rgba(var(--danger-rgb), 0.04);
}
.danger-zone > div { flex: 1; }
.danger-zone h3 { margin: 0 0 2px; font-size: 14.5px; color: var(--danger-text); }
.danger-zone p { margin: 0; font-size: 13px; color: var(--muted); }

/* ---------- Toast ---------- */

.toasts {
  position: fixed;
  left: 50%;
  bottom: calc(24px + env(safe-area-inset-bottom));
  z-index: 100;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  transform: translateX(-50%);
  pointer-events: none;
  width: max-content;
  max-width: calc(100vw - 32px);
}
.toast {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  border-radius: 999px;
  border: 1px solid var(--border-strong);
  background: var(--toast-bg);
  -webkit-backdrop-filter: blur(12px);
  backdrop-filter: blur(12px);
  box-shadow: var(--shadow);
  font-size: 13.5px;
  animation: toastIn 0.4s var(--ease-spring) both;
}
.toast.leaving { animation: toastOut 0.3s var(--ease) both; }
.toast .ic { color: var(--accent); }
.toast-success .ic { color: var(--success); }
.toast-error { border-color: rgba(var(--danger-rgb), 0.4); }
.toast-error .ic { color: var(--danger); }

/* =========================================================
   ANIMAZIONI
   ========================================================= */

@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes fadeOut { from { opacity: 1; } to { opacity: 0; } }
@keyframes fadeSlideUp { from { opacity: 0; transform: translateY(12px); } to { opacity: 1; transform: none; } }
@keyframes fadeSlideIn { from { opacity: 0; transform: translateX(-8px); } to { opacity: 1; transform: none; } }
@keyframes modalIn { from { opacity: 0; transform: translateY(14px) scale(0.97); } to { opacity: 1; transform: none; } }
@keyframes modalOut { from { opacity: 1; transform: none; } to { opacity: 0; transform: translateY(8px) scale(0.98); } }
@keyframes toastIn { from { opacity: 0; transform: translateY(16px) scale(0.9); } to { opacity: 1; transform: none; } }
@keyframes toastOut { to { opacity: 0; transform: translateY(8px) scale(0.95); } }
@keyframes popIn { from { opacity: 0; transform: scale(0.6); } to { opacity: 1; transform: none; } }
@keyframes blink { to { visibility: hidden; } }
@keyframes bounce { 0%, 80%, 100% { transform: translateY(0); opacity: 0.4; } 40% { transform: translateY(-6px); opacity: 1; } }
@keyframes ping { 75%, 100% { transform: scale(2.4); opacity: 0; } }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.4; } }
@keyframes shimmer { from { background-position: 200% 0; } to { background-position: -200% 0; } }
@keyframes breathe { 0%, 100% { opacity: 0.25; transform: scale(0.95); } 50% { opacity: 0.45; transform: scale(1.05); } }
@keyframes float { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-6px); } }
@keyframes glowPulse { 0%, 100% { box-shadow: 0 0 0 0 rgba(var(--accent-rgb), 0.5); } 50% { box-shadow: 0 0 0 6px rgba(var(--accent-rgb), 0); } }
@keyframes shake { 0%, 100% { transform: translateX(0); } 25% { transform: translateX(-4px); } 75% { transform: translateX(4px); } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
  .cursor { animation: none; }
}

/* =========================================================
   RESPONSIVE
   ========================================================= */

@media (max-width: 860px) {
  .app, .app.sidebar-collapsed { grid-template-columns: minmax(0, 1fr); }

  .sidebar {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    z-index: 40;
    width: min(86vw, 310px);
    padding-top: calc(12px + env(safe-area-inset-top));
    transform: translateX(-102%);
    transition: transform 0.38s var(--ease);
    box-shadow: none;
  }
  .sidebar > * { min-width: 0; }
  .app.sidebar-open .sidebar { transform: none; box-shadow: 20px 0 60px -10px rgba(0, 0, 0, 0.7); }

  .overlay {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 30;
    background: rgba(0, 0, 0, 0.55);
    -webkit-backdrop-filter: blur(3px);
    backdrop-filter: blur(3px);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s;
  }
  .app.sidebar-open .overlay { opacity: 1; pointer-events: auto; }

  .btn-menu, .btn-new-top { display: inline-grid; }
  .new-chat kbd { display: none; }
  .conv-actions { opacity: 1; transform: none; }

  .topbar { padding: 0 8px; height: 54px; gap: 2px; }
  .topbar-title { padding-left: 2px; }
  .btn-new-top { order: 3; }

  .messages { padding: 8px 16px 28px; }
  .composer-wrap { padding: 0 12px calc(10px + env(safe-area-inset-bottom)); }
  .composer { padding-left: 16px; }
  .scroll-bottom { bottom: 108px; }
}

@media (max-width: 600px) {
  body { font-size: 15px; }
  .model-tag { display: none; }
  .status { padding: 0 10px; height: 30px; }
  .status-online .status-label,
  .status-offline .status-label,
  .status-checking .status-label { display: none; }

  .welcome { padding: 16px; }
  .welcome-sub { font-size: 15px; }
  .suggestions { grid-template-columns: 1fr; gap: 8px; margin-top: 24px; }
  .suggestion { padding: 12px 14px; }
  .demo-banner { border-radius: var(--radius); font-size: 12.5px; }

  .msg-assistant { grid-template-columns: minmax(0, 1fr); gap: 6px; }
  .msg-assistant .avatar { display: none; }
  .msg-bubble { max-width: 90%; }
  .msg-user .msg-actions { opacity: 1; }
  .code-block pre { font-size: 12.5px; padding: 12px; }

  .disclaimer { font-size: 11px; }

  .modal { padding: 0; align-items: flex-end; }
  .modal-panel {
    max-height: 92dvh;
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    border-bottom: 0;
    animation-name: sheetIn;
    padding-bottom: env(safe-area-inset-bottom);
  }
  .modal.closing .modal-panel { animation-name: sheetOut; }
  .modal-sm { max-width: none; }
  .modal-body { padding: 18px 16px; }
  .tab span { display: none; }
  .tab { padding: 10px 14px 12px; }
  .tab.active span { display: inline; }
  .field-grid { grid-template-columns: 1fr; gap: 0; }
  .modal-footer { flex-wrap: wrap; }
  #btnResetSettings { order: 5; width: 100%; }
  .danger-zone { flex-direction: column; align-items: stretch; }
}

@keyframes sheetIn { from { transform: translateY(100%); } to { transform: none; } }
@keyframes sheetOut { to { transform: translateY(100%); } }

/* =========================================================
   PERLA — sfera animata, aurora, vetro e fluidità
   ========================================================= */

/* ---------- Sfondo aurora (blob sfocati che fluttuano lentamente) ---------- */

.bg-glow { background: none; overflow: hidden; }
.bg-glow span {
  position: absolute;
  border-radius: 50%;
  filter: blur(90px);
  opacity: calc(0.16 * var(--aurora-opacity));
  will-change: transform;
}
.bg-glow span:nth-child(1) {
  width: 46vmax; height: 46vmax; left: 45%; top: -18vmax;
  background: var(--accent);
  animation: drift1 26s ease-in-out infinite alternate;
}
.bg-glow span:nth-child(2) {
  width: 38vmax; height: 38vmax; left: 10%; bottom: -20vmax;
  background: var(--accent-2);
  opacity: calc(0.10 * var(--aurora-opacity));
  animation: drift2 32s ease-in-out infinite alternate;
}
.bg-glow span:nth-child(3) {
  width: 28vmax; height: 28vmax; right: -8vmax; top: 40%;
  background: #67e8f9;
  opacity: calc(0.07 * var(--aurora-opacity));
  animation: drift3 38s ease-in-out infinite alternate;
}
@keyframes drift1 { to { transform: translate(-14vmax, 8vmax) scale(1.15); } }
@keyframes drift2 { to { transform: translate(16vmax, -10vmax) scale(0.9); } }
@keyframes drift3 { to { transform: translate(-12vmax, -14vmax) scale(1.2); } }

/* ---------- Sfera ---------- */

.orb { position: relative; flex-shrink: 0; pointer-events: none; }
.orb-canvas { display: block; width: 100%; height: 100%; }

.orb-hero {
  --size: clamp(150px, 30vh, 280px);
  width: var(--size);
  height: var(--size);
  margin: 0 auto 6px;
  animation: orbIn 1.1s var(--ease) both, orbFloat 7s ease-in-out 1.1s infinite;
}
@keyframes orbIn { from { opacity: 0; transform: scale(0.6); filter: blur(12px); } to { opacity: 1; transform: none; filter: none; } }
@keyframes orbFloat { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-8px); } }

.orb-mini {
  width: 40px;
  height: 40px;
  align-self: flex-end;
  margin: 0 -2px 0 -6px;
  transition: transform 0.6s var(--ease-spring);
}
.is-generating .orb-mini { transform: scale(1.22); }

/* Fallback senza WebGL: gradienti CSS animati */
.orb-css {
  position: absolute;
  inset: 11%;
  border-radius: 50%;
  overflow: hidden;
  background:
    radial-gradient(circle at 32% 28%, #fff 0 8%, transparent 30%),
    radial-gradient(circle at 70% 75%, rgba(103, 232, 249, 0.7), transparent 55%),
    radial-gradient(circle at 50% 50%, #eae6ff, var(--orb-a, #a78bfa) 85%);
  box-shadow: 0 0 40px 4px rgba(var(--accent-rgb), 0.45), inset 0 0 20px rgba(255, 255, 255, 0.6);
  animation: cssOrb 6s ease-in-out infinite;
}
.orb-css span {
  position: absolute;
  inset: -30%;
  background: conic-gradient(from 0deg, transparent, var(--orb-b, #f0abfc), transparent 40%, #67e8f9, transparent 70%);
  opacity: 0.45;
  filter: blur(14px);
  mix-blend-mode: screen;
  animation: spin 9s linear infinite;
}
.orb-css span:nth-child(2) { animation-duration: 13s; animation-direction: reverse; opacity: 0.3; }
.orb-css span:nth-child(3) { animation-duration: 21s; opacity: 0.2; }
.orb[data-state="thinking"] .orb-css span { animation-duration: 2.5s; }
.orb[data-state="speaking"] .orb-css { animation-duration: 1.2s; }
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes cssOrb { 0%, 100% { transform: scale(1); border-radius: 50%; } 50% { transform: scale(1.04); border-radius: 48% 52% 50% 50%; } }

/* ---------- Schermata vuota: input al centro, sotto la sfera ---------- */

.welcome { padding-top: 8px; }
.welcome h1 { margin-top: 4px; }
.composer-slot { margin: 26px auto 0; max-width: var(--content-w); }
.suggestions { margin-top: 18px; }
.demo-banner { margin-top: 14px; }

.composer-wrap {
  transform: translateY(calc(var(--lift, 0px) * -1));
  will-change: transform;
}
body.ready .composer-wrap { transition: transform 0.75s var(--ease); }
/* Da vuota la chat occupa tutta l'altezza: l'input "galleggia" sopra */
.app.is-empty .composer-wrap { position: absolute; left: 0; right: 0; bottom: 0; }
.app.is-empty .chat { padding-bottom: 24px; }
.app.is-empty .composer-wrap::before { opacity: 0; }
.composer-wrap::before { transition: opacity 0.4s; }
.app.is-empty .disclaimer { opacity: 0; }
.disclaimer { transition: opacity 0.4s; }

/* ---------- Barra di input in vetro smerigliato ---------- */

.composer {
  padding-left: 8px;
  align-items: flex-end;
  background: var(--glass);
  -webkit-backdrop-filter: blur(22px) saturate(1.5);
  backdrop-filter: blur(22px) saturate(1.5);
  border-color: rgba(var(--fg-rgb), 0.10);
  box-shadow:
    0 24px 60px -18px rgba(0, 0, 0, 0.75),
    0 0 0 1px rgba(var(--fg-rgb), 0.02) inset,
    0 1px 0 rgba(var(--fg-rgb), 0.07) inset;
  transition: border-color 0.35s var(--ease), box-shadow 0.45s var(--ease), background 0.35s;
}
.composer:focus-within {
  background: var(--glass-focus);
  border-color: rgba(var(--accent-rgb), 0.45);
  box-shadow:
    0 24px 60px -18px rgba(0, 0, 0, 0.75),
    0 0 0 4px rgba(var(--accent-rgb), 0.10),
    0 0 50px -10px rgba(var(--accent-rgb), 0.35),
    0 1px 0 rgba(var(--fg-rgb), 0.08) inset;
}
.composer textarea { padding-left: 4px; }

/* Pulsante invio in stile perla */
.send-btn {
  background: radial-gradient(circle at 30% 25%, #fff 0, #ece8ff 30%, var(--accent) 100%);
  color: #1b1530;
  box-shadow: 0 6px 22px -6px rgba(var(--accent-rgb), 0.9), inset 0 1px 1px rgba(255, 255, 255, 0.8);
}
.send-btn.is-stop { background: var(--stop-bg); color: var(--stop-fg); }

/* ---------- Fluidità generale ---------- */

.msg-enter { animation: msgIn 0.6s var(--ease) both; }
@keyframes msgIn {
  from { opacity: 0; transform: translateY(14px) scale(0.985); filter: blur(6px); }
  to { opacity: 1; transform: none; filter: none; }
}

.msg-bubble {
  background: var(--bubble);
  border-color: rgba(var(--fg-rgb), 0.08);
}

.sidebar { background: var(--sidebar-glass); -webkit-backdrop-filter: blur(20px); backdrop-filter: blur(20px); }
.topbar { background: linear-gradient(180deg, rgba(var(--bg-rgb), 0.85) 30%, rgba(var(--bg-rgb), 0)); }

.suggestion {
  background: rgba(var(--fg-rgb), 0.025);
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
}

.modal-panel {
  background: var(--modal-bg);
  -webkit-backdrop-filter: blur(24px) saturate(1.4);
  backdrop-filter: blur(24px) saturate(1.4);
}

@media (max-width: 600px) {
  .orb-hero { --size: clamp(140px, 30vh, 210px); }
  .composer-slot { display: none; }
  .app.is-empty .chat { padding-bottom: 120px; }
  .orb-mini { width: 36px; height: 36px; }
  .bg-glow span { filter: blur(60px); }
}

/* ---------- Ritocchi specifici del tema chiaro ---------- */

:root[data-theme="light"] .grad-text {
  background-image: linear-gradient(135deg, color-mix(in srgb, var(--accent) 70%, #1c1930), var(--accent) 60%, color-mix(in srgb, var(--accent-2) 80%, #1c1930));
}
:root[data-theme="light"] .markdown a,
:root[data-theme="light"] .link-btn { color: color-mix(in srgb, var(--accent) 72%, #1c1930); }
:root[data-theme="light"] .markdown a { text-decoration-color: rgba(var(--accent-rgb), 0.45); }
:root[data-theme="light"] .sg-icon,
:root[data-theme="light"] .value-badge,
:root[data-theme="light"] .tab.active .ic,
:root[data-theme="light"] .new-chat .ic { color: color-mix(in srgb, var(--accent) 80%, #1c1930); }
:root[data-theme="light"] .markdown ul li::marker { color: color-mix(in srgb, var(--accent) 85%, #1c1930); }
:root[data-theme="light"] .composer {
  box-shadow: 0 24px 60px -20px rgba(76, 52, 160, 0.28), 0 1px 0 rgba(255, 255, 255, 0.9) inset;
}
:root[data-theme="light"] .composer:focus-within {
  box-shadow: 0 24px 60px -20px rgba(76, 52, 160, 0.32), 0 0 0 4px rgba(var(--accent-rgb), 0.14), 0 0 50px -10px rgba(var(--accent-rgb), 0.35);
}
:root[data-theme="light"] .msg-bubble { box-shadow: 0 6px 20px -10px rgba(76, 52, 160, 0.25); }
:root[data-theme="light"] .demo-banner { background: rgba(251, 191, 36, 0.12); border-color: rgba(212, 138, 6, 0.3); }
:root[data-theme="light"] .status-demo { border-color: rgba(212, 138, 6, 0.35); }
:root[data-theme="light"] .status-online { border-color: rgba(18, 168, 122, 0.35); }
:root[data-theme="light"] .modal-panel { box-shadow: 0 30px 80px -20px rgba(60, 40, 130, 0.35); }
:root[data-theme="light"] .orb-hero { filter: drop-shadow(0 18px 40px rgba(var(--accent-rgb), 0.25)); }

/* ---------- Selettore tema ---------- */

.segmented {
  display: inline-grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  gap: 4px;
  padding: 4px;
  border-radius: 12px;
  background: rgba(var(--fg-rgb), 0.05);
  border: 1px solid var(--border);
  width: 100%;
  max-width: 420px;
}
.segmented button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  height: 36px;
  padding: 0 12px;
  border-radius: 9px;
  color: var(--text-2);
  font-size: 13.5px;
  font-weight: 550;
  transition: background 0.25s var(--ease), color 0.25s, box-shadow 0.25s;
}
.segmented button:hover { color: var(--text); }
.segmented button.active {
  background: var(--panel);
  color: var(--text);
  box-shadow: var(--shadow-sm), 0 0 0 1px var(--border);
}
:root[data-theme="light"] .send-btn:disabled {
  background: var(--panel-3);
  color: var(--muted);
  opacity: 1;
  filter: none;
  box-shadow: none;
}
