:root {
  --bg: #0a0e14;
  --bg-soft: #0f141d;
  --bg-elev: #121925;
  --fg: #e6edf3;
  --muted: #7d8da3;
  --line: #1c2530;
  --accent: #22d3ee;
  --accent-dim: #0e7490;
  --display: "Space Grotesk", "Inter", system-ui, sans-serif;
  --sans: "Inter", system-ui, -apple-system, sans-serif;
  --mono: "JetBrains Mono", ui-monospace, "SFMono-Regular", monospace;
  --sidebar-w: 300px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }

body {
  background-color: var(--bg);
  color: var(--fg);
  font-family: var(--sans);
  font-size: 16px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

.logo {
  font-family: var(--mono);
  font-weight: 700;
  font-size: 18px;
  letter-spacing: -0.01em;
  color: var(--fg);
  text-decoration: none;
}
.logo .dot { color: var(--accent); }

.status-dot {
  display: inline-block;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 8px rgba(34, 211, 238, 0.7);
}

.icon-btn {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  transition: color 0.15s ease, border-color 0.15s ease, background 0.15s ease;
}
.icon-btn:hover {
  color: var(--fg);
  border-color: var(--accent-dim);
  background: rgba(34, 211, 238, 0.06);
}

/* layout */
.app {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  height: 100vh;
  height: 100dvh;
}

/* sidebar */
.sidebar {
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--line);
  background: var(--bg-soft);
  min-height: 0;
}
.sidebar__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 18px 16px;
  border-bottom: 1px solid var(--line);
}
.conv-list {
  flex: 1;
  overflow-y: auto;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.conv-empty {
  color: var(--muted);
  font-size: 14px;
  padding: 14px;
}
.conv-item {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 3px;
  width: 100%;
  text-align: left;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 11px 13px;
  cursor: pointer;
  color: var(--fg);
  transition: background 0.15s ease, border-color 0.15s ease;
}
.conv-item:hover { background: var(--bg-elev); }
.conv-item.is-active {
  background: var(--bg-elev);
  border-color: var(--accent-dim);
}
.conv-item__title {
  font-size: 14px;
  font-weight: 500;
  line-height: 1.35;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}
/* Il pallino sta fuori dal flusso del titolo: dentro verrebbe mangiato
   dall'ellipsis quando il testo è lungo. */
.conv-item__unread {
  position: absolute;
  top: 12px;
  right: 10px;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 0 3px var(--bg-soft);
}
.conv-item__time {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--muted);
}
.sidebar__foot {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 14px 18px;
  border-top: 1px solid var(--line);
  font-family: var(--mono);
  font-size: 11px;
  color: var(--muted);
  letter-spacing: 0.02em;
}

/* chat */
.chat {
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
}
.chat__head {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px 22px;
  border-bottom: 1px solid var(--line);
}
.chat__menu { display: none; }
.chat__title {
  font-family: var(--display);
  font-weight: 600;
  font-size: 16px;
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.chat__badge {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  font-family: var(--mono);
  font-size: 12px;
  color: var(--muted);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 5px 11px;
  white-space: nowrap;
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 28px clamp(18px, 6vw, 80px);
  display: flex;
  flex-direction: column;
  gap: 22px;
}

.chat-empty {
  margin: auto;
  text-align: center;
}
.chat-empty__title {
  font-family: var(--display);
  font-weight: 700;
  font-size: clamp(24px, 4vw, 34px);
  letter-spacing: -0.02em;
}
.chat-empty__sub {
  color: var(--muted);
  margin-top: 10px;
}

/* message bubbles */
.msg {
  max-width: 760px;
  width: 100%;
  margin: 0 auto;
}
.msg__meta {
  display: flex;
  align-items: baseline;
  gap: 10px;
  margin-bottom: 6px;
}
.msg__who {
  font-family: var(--mono);
  font-size: 12px;
  letter-spacing: 0.03em;
  color: var(--accent);
}
.msg--user .msg__who { color: var(--fg); }
.msg__time {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--muted);
}
.msg__body {
  font-size: 15.5px;
  line-height: 1.6;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 14px 16px;
  background: var(--bg-soft);
}
.msg--user .msg__body {
  background: var(--bg-elev);
  border-color: #233042;
}
.msg--assistant .msg__body,
.msg--dev .msg__body {
  background: var(--bg-soft);
}

/* in volo: la bolla c'è già ma il server non ha ancora confermato */
.msg--sending { opacity: 0.55; }

/* invio fallito: il testo resta a schermo, con un modo per ritentare */
.msg--failed .msg__body {
  border-color: #7f1d1d;
  background: #1a1113;
}
.msg__time--err { color: #f87171; }
.msg__retry {
  margin-top: 8px;
  font-family: var(--mono);
  font-size: 12px;
  color: var(--accent);
  background: none;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 6px 12px;
  cursor: pointer;
  transition: border-color 0.18s ease, color 0.18s ease;
}
.msg__retry:hover {
  border-color: var(--accent-dim);
  color: var(--fg);
}

/* avviso di servizio, non un messaggio: nessuno l'ha scritto */
.msg-notice {
  max-width: 760px;
  width: 100%;
  margin: 0 auto;
  font-size: 13.5px;
  line-height: 1.55;
  color: var(--muted);
  text-align: center;
  padding: 4px 8px;
}

/* richiesta email, dopo il primo messaggio */
.email-prompt {
  max-width: 760px;
  width: 100%;
  margin: 0 auto;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--bg-soft);
  padding: 16px;
}
.email-prompt__text {
  font-size: 14px;
  line-height: 1.5;
  color: var(--fg);
  margin: 0 0 12px;
}
.email-prompt__row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}
.email-prompt__input {
  flex: 1 1 200px;
  min-width: 0;
  font-family: var(--sans);
  font-size: 14px;
  color: var(--fg);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  outline: none;
  transition: border-color 0.18s ease;
}
.email-prompt__input:focus { border-color: var(--accent-dim); }
.email-prompt__input::placeholder { color: var(--muted); }
.email-prompt__send {
  font-family: var(--mono);
  font-size: 13px;
  color: var(--bg);
  background: var(--accent);
  border: none;
  border-radius: 10px;
  padding: 10px 18px;
  cursor: pointer;
  transition: opacity 0.18s ease;
}
.email-prompt__send:hover { opacity: 0.88; }
.email-prompt__send:disabled { opacity: 0.5; cursor: default; }
.email-prompt__err {
  font-size: 13px;
  color: #f87171;
  margin: 10px 0 0;
}

/* composer */
.composer-wrap {
  padding: 14px clamp(18px, 6vw, 80px) 20px;
  border-top: 1px solid var(--line);
}
.composer {
  position: relative;
  display: flex;
  align-items: flex-end;
  gap: 10px;
  max-width: 760px;
  margin: 0 auto;
  background: var(--bg-soft);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 12px 12px 12px 16px;
  transition: border-color 0.18s ease, box-shadow 0.18s ease;
}
.composer:focus-within {
  border-color: var(--accent-dim);
  box-shadow: 0 0 0 3px rgba(34, 211, 238, 0.10);
}
.composer__input {
  flex: 1;
  resize: none;
  border: none;
  outline: none;
  background: transparent;
  color: var(--fg);
  font-family: var(--sans);
  font-size: 15.5px;
  line-height: 1.5;
  max-height: 200px;
  overflow-y: auto;
  padding: 5px 0;
}
.composer__input::placeholder { color: var(--muted); }
.composer__send {
  flex: 0 0 auto;
  width: 36px;
  height: 36px;
  border: none;
  border-radius: 50%;
  background: var(--accent);
  color: var(--bg);
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: transform 0.15s ease;
}
.composer__send:hover { transform: translateY(-1px); }
.composer__hint {
  max-width: 760px;
  margin: 10px auto 0;
  text-align: center;
  font-size: 12px;
  color: var(--muted);
}

/* mobile */
@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: min(86vw, var(--sidebar-w));
    transform: translateX(-100%);
    transition: transform 0.22s ease;
    z-index: 20;
  }
  .app.sidebar-open .sidebar { transform: translateX(0); }
  .app.sidebar-open::after {
    content: "";
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 15;
  }
  .chat__menu { display: grid; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
