/* ================================================================
   Leadgen — Design System
   Fonts: Bricolage Grotesque (display) · Hanken Grotesk (body/UI)
          JetBrains Mono (data/mono)
   ================================================================ */

:root {
  /* Core palette */
  --bg:            #f7f6f3;
  --surface:       #ffffff;
  --ink:           #15171c;
  --muted:         #6c727e;
  --line:          #e7e4dd;

  /* Sidebar */
  --sidebar:       #14161b;
  --sidebar-ink:   #aeb4c0;
  --sidebar-ink-soft: #7c8290;
  --sidebar-w:     230px;

  /* Accent */
  --accent:        #4f46e5;
  --accent-press:  #4338ca;
  --accent-soft:   #ecedff;

  /* Semantic */
  --ok:            #15a34a;
  --warn:          #b45309;
  --danger:        #dc2626;

  /* Shape */
  --radius:        12px;
  --radius-sm:     8px;
  --shadow:        0 1px 2px rgba(20,22,27,.06), 0 8px 24px -12px rgba(20,22,27,.12);

  /* Aliases for backward-compat with any remaining refs */
  --blu:           var(--accent);
  --bordo:         var(--line);
  --grigio:        var(--bg);
}

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

html { height: 100%; }

body {
  font-family: "Hanken Grotesk", system-ui, -apple-system, sans-serif;
  font-size: 15px;
  line-height: 1.55;
  color: var(--ink);
  background: var(--bg);
  min-height: 100vh;
  display: flex;
}

/* ================================================================
   LAYOUT — sidebar + content
   ================================================================ */

.sidebar {
  position: fixed;
  top: 0; left: 0; bottom: 0;
  width: var(--sidebar-w);
  background: var(--sidebar);
  display: flex;
  flex-direction: column;
  z-index: 100;
  /* subtle gradient for depth */
  background-image: linear-gradient(180deg, #1a1d24 0%, #14161b 100%);
}

.sidebar-wordmark {
  font-family: "Bricolage Grotesque", system-ui, sans-serif;
  font-weight: 700;
  font-size: 1.3rem;
  color: #ffffff;
  letter-spacing: -.01em;
  padding: 1.4rem 1.25rem 1.2rem;
  border-bottom: 1px solid rgba(255,255,255,.07);
}

/* ---- Sidebar nav ---- */
nav {
  flex: 1;
  padding: .75rem .6rem;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

nav button {
  display: flex;
  align-items: center;
  gap: .6rem;
  width: 100%;
  padding: .55rem .75rem;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--sidebar-ink);
  font-family: "Hanken Grotesk", system-ui, sans-serif;
  font-size: .875rem;
  font-weight: 500;
  cursor: pointer;
  text-align: left;
  transition: background 140ms ease, color 140ms ease;
  position: relative;
}

nav button:hover {
  background: rgba(255,255,255,.06);
  color: #d8dce6;
}

nav button.attivo {
  background: rgba(79, 70, 229, .18);
  color: #ffffff;
}

nav button.attivo::before {
  content: "";
  position: absolute;
  left: -2px;
  top: 20%;
  bottom: 20%;
  width: 3px;
  border-radius: 0 3px 3px 0;
  background: var(--accent);
}

.nav-icon {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
  opacity: .75;
  transition: opacity 140ms ease;
}

nav button.attivo .nav-icon,
nav button:hover .nav-icon {
  opacity: 1;
}

/* ---- Sidebar footer — integrazioni ---- */
.sidebar-footer {
  padding: .9rem 1rem 1.1rem;
  border-top: 1px solid rgba(255,255,255,.07);
}

#stato-integrazioni {
  display: block;
  font-size: .75rem;
  color: var(--sidebar-ink-soft);
  line-height: 1.8;
  white-space: pre-line;  /* keeps newlines if JS writes them */
}

/* ================================================================
   MAIN CONTENT AREA
   ================================================================ */

.layout-content {
  margin-left: var(--sidebar-w);
  flex: 1;
  min-width: 0;
}

main {
  padding: 2rem 2.25rem;
  max-width: 1040px;
}

/* ================================================================
   TABS
   ================================================================ */

.tab {
  display: none;
  animation: fadeup .22s ease;
}

.tab.attivo {
  display: block;
}

@keyframes fadeup {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ---- Tab header ---- */
.tab-header {
  margin-bottom: 1.5rem;
}

.tab-header h2 {
  font-family: "Bricolage Grotesque", system-ui, sans-serif;
  font-size: 1.55rem;
  font-weight: 700;
  color: var(--ink);
  letter-spacing: -.02em;
  line-height: 1.2;
  margin-bottom: .3rem;
}

.muted {
  color: var(--muted);
  font-size: .875rem;
  font-weight: 400;
}

/* ================================================================
   CARDS
   ================================================================ */

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 1.25rem 1.4rem;
  box-shadow: var(--shadow);
  margin-bottom: 1rem;
}

.card-table {
  padding: 0;
  overflow: hidden;
}

.card-table table {
  margin-top: 0;
}

/* ================================================================
   TYPOGRAPHY
   ================================================================ */

h2 {
  font-family: "Bricolage Grotesque", system-ui, sans-serif;
  font-weight: 700;
  font-size: 1.55rem;
  color: var(--ink);
  letter-spacing: -.02em;
}

strong { font-weight: 600; }

.stat-line {
  font-size: .9rem;
  color: var(--muted);
  margin-bottom: 1rem;
}
.stat-line strong { color: var(--ink); }

/* ================================================================
   FORM ELEMENTS
   ================================================================ */

input, select, textarea {
  font-family: "Hanken Grotesk", system-ui, sans-serif;
  font-size: .875rem;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 0 .75rem;
  height: 40px;
  width: 100%;
  transition: border-color 120ms ease, box-shadow 120ms ease;
  appearance: none;
}

select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1 1l5 5 5-5' stroke='%236c727e' stroke-width='1.5' fill='none' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right .7rem center;
  padding-right: 2rem;
}

textarea {
  height: auto;
  padding: .6rem .75rem;
  resize: vertical;
  line-height: 1.5;
}

input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

input::placeholder, textarea::placeholder { color: #b0b5bf; }

/* contenteditable cells */
[contenteditable]:focus {
  outline: 2px solid var(--accent);
  background: var(--accent-soft);
  border-radius: 4px;
}

/* ---- Field row layout ---- */
.field-row {
  display: flex;
  align-items: center;
  gap: .75rem;
  margin-bottom: .75rem;
}

.field-row label {
  font-size: .8rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: .04em;
  white-space: nowrap;
  min-width: 120px;
}

.field-row input,
.field-row select,
.field-row textarea {
  flex: 1;
}

.field-row--col {
  flex-direction: column;
  align-items: flex-start;
}

.field-row--col label {
  min-width: unset;
  margin-bottom: .25rem;
}

.field-row--col textarea,
.field-row--col input {
  width: 100%;
}

/* ---- Form inline (search bar style) ---- */
.form-inline {
  display: flex;
  flex-wrap: wrap;
  gap: .6rem;
  align-items: flex-end;
}

.form-inline input,
.form-inline select {
  width: auto;
  flex: 1;
  min-width: 160px;
}

/* ---- Form stack ---- */
.form-stack {
  display: flex;
  flex-direction: column;
  gap: .1rem;
}

.form-actions {
  display: flex;
  gap: .6rem;
  flex-wrap: wrap;
  padding-top: .6rem;
}

/* ================================================================
   BUTTONS
   ================================================================ */

button {
  font-family: "Hanken Grotesk", system-ui, sans-serif;
  font-size: .875rem;
  font-weight: 600;
  border: none;
  border-radius: 999px;
  padding: 0 1.1rem;
  height: 38px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background 130ms ease, box-shadow 130ms ease, transform 80ms ease;
  white-space: nowrap;
}

button:active { transform: translateY(1px); }

.btn-primary, button[type="submit"]:not(.btn-secondary) {
  background: var(--accent);
  color: #ffffff;
}

.btn-primary:hover,
button[type="submit"]:not(.btn-secondary):hover {
  background: var(--accent-press);
}

.btn-secondary {
  background: var(--surface);
  color: var(--ink);
  border: 1px solid var(--line);
  box-shadow: 0 1px 2px rgba(20,22,27,.06);
}

.btn-secondary:hover {
  background: var(--bg);
  border-color: #ccc;
}

button.pericolo {
  background: var(--danger);
  color: #ffffff;
  border-radius: 999px;
}

button.pericolo:hover { background: #b91c1c; }

button[hidden] { display: none; }

/* ---- Link button ---- */
a.bottone-link {
  display: inline-flex;
  align-items: center;
  height: 38px;
  padding: 0 1rem;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--line);
  color: var(--accent);
  text-decoration: none;
  font-size: .875rem;
  font-weight: 600;
  white-space: nowrap;
  transition: background 130ms ease;
}

a.bottone-link:hover { background: var(--bg); }
a { color: var(--accent); }

/* ================================================================
   TABLES
   ================================================================ */

table {
  width: 100%;
  border-collapse: collapse;
  font-size: .83rem;
}

thead tr {
  background: var(--bg);
}

th {
  text-align: left;
  padding: .6rem 1rem;
  font-size: .7rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: .06em;
  border-bottom: 1px solid var(--line);
}

td {
  padding: .55rem 1rem;
  border-bottom: 1px solid var(--line);
  vertical-align: middle;
  color: var(--ink);
}

tbody tr:last-child td { border-bottom: none; }

tbody tr:hover td { background: #fafaf8; }

/* phone / numeric columns */
td:nth-child(4) {
  font-family: "JetBrains Mono", monospace;
  font-size: .8rem;
}

#tabella-lead th:first-child,
#tabella-lead td:first-child {
  text-align: center;
  width: 2.4rem;
  padding-left: .75rem;
  padding-right: .5rem;
}

/* ---- Estado badge (text only, colored) ---- */
td:has([data-estado]) { /* noop for now */ }

/* ================================================================
   ESITO / STATUS LINE
   ================================================================ */

.esito {
  margin: .6rem 0 0;
  font-size: .875rem;
  color: var(--muted);
  min-height: 1.3em;
}

/* ================================================================
   FILTRI LEAD
   ================================================================ */

.barra-filtri {
  display: flex;
  flex-wrap: wrap;
  gap: .6rem 1rem;
  align-items: center;
  justify-content: space-between;
}

.filtri-campi,
.filtri-azioni {
  display: flex;
  flex-wrap: wrap;
  gap: .5rem;
  align-items: center;
}

.barra-filtri select,
.barra-filtri input {
  height: 38px;
  width: auto;
}

.barra-filtri input[name="comune"] { width: 140px; }

/* ================================================================
   AI BOX
   ================================================================ */

.ai-box {
  background: #fafaff;
  border: 1px dashed #c9c6f0;
  border-radius: var(--radius);
  padding: .9rem 1.1rem;
  margin-bottom: 1rem;
}

.ai-box summary {
  cursor: pointer;
  font-size: .875rem;
  font-weight: 600;
  color: var(--accent);
  list-style: none;
  display: flex;
  align-items: center;
  user-select: none;
}

.ai-box summary::-webkit-details-marker { display: none; }

.ai-box[open] summary { margin-bottom: .85rem; }

.ai-fields {
  display: flex;
  flex-direction: column;
  gap: .5rem;
}

.ai-actions {
  display: flex;
  flex-wrap: wrap;
  gap: .5rem;
  align-items: center;
}

.ai-actions select { width: auto; }

/* ================================================================
   PROGRESS
   ================================================================ */

progress {
  width: 100%;
  margin-top: .9rem;
  height: 6px;
  border-radius: 999px;
  border: none;
  appearance: none;
  background: var(--line);
  overflow: hidden;
}

progress::-webkit-progress-bar { background: var(--line); border-radius: 999px; }
progress::-webkit-progress-value { background: var(--accent); border-radius: 999px; transition: width .3s ease; }
progress::-moz-progress-bar { background: var(--accent); border-radius: 999px; }

/* ================================================================
   DIVIDER
   ================================================================ */

.divider {
  border-top: 1px solid var(--line);
  margin: 1.1rem 0;
}

/* ================================================================
   DA VERIFICARE (warning email)
   ================================================================ */

.da-verificare {
  color: var(--warn);
  font-weight: 600;
}
.da-verificare::after { content: " ⚠"; }

/* ================================================================
   CAMPAGNA — anteprime WA
   ================================================================ */

.anteprime-list {
  margin-top: .9rem;
  display: flex;
  flex-direction: column;
  gap: .5rem;
}

.anteprime-list p {
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: .55rem .85rem;
  font-size: .85rem;
  margin: 0;
}

/* ================================================================
   CHAT WHATSAPP
   ================================================================ */

.chat-filtro-bar {
  display: flex;
  align-items: center;
  gap: .6rem;
  margin-bottom: .9rem;
  font-size: .82rem;
  color: var(--muted);
  font-weight: 500;
}

.chat-filtro-bar select {
  height: 34px;
  font-size: .82rem;
  width: auto;
}

.chat-wrap {
  display: flex;
  gap: 0;
  height: 62vh;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

/* ---- Contact list panel ---- */
.chat-contatti {
  width: 240px;
  flex-shrink: 0;
  overflow-y: auto;
  border-right: 1px solid var(--line);
  background: #fafaf8;
}

.chat-gruppo-titolo {
  font-size: .68rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .07em;
  color: var(--muted);
  padding: .65rem .85rem .35rem;
  border-bottom: 1px solid var(--line);
  background: var(--bg);
  position: sticky;
  top: 0;
  z-index: 1;
}

.chat-contatto {
  padding: .6rem .85rem;
  cursor: pointer;
  border-bottom: 1px solid var(--line);
  transition: background 100ms ease;
}

.chat-contatto:hover { background: var(--accent-soft); }
.chat-contatto.attivo { background: #e8e7f9; }

.chat-contatto-nome {
  font-weight: 600;
  font-size: .875rem;
  color: var(--ink);
  display: flex;
  align-items: center;
  gap: .3rem;
}

.chat-contatto-preview {
  font-size: .75rem;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: .1rem;
}

/* contattabili */
.chat-contattabile {
  border-left: 3px solid #a8c4ff;
}
.chat-contattabile:hover { background: var(--accent-soft); }
.chat-contattabile.attivo { background: #e8e7f9; }

/* pallino non-letti */
.pallino-nonletti {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--danger);
  flex-shrink: 0;
}

/* ---- Thread panel ---- */
.chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.chat-thread {
  flex: 1;
  overflow-y: auto;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: .45rem;
  background: #f9f8f4;
}

.msg-in, .msg-out {
  max-width: 68%;
  padding: .55rem .8rem;
  border-radius: 14px;
  font-size: .875rem;
  word-break: break-word;
  line-height: 1.45;
}

.msg-in {
  align-self: flex-start;
  background: var(--surface);
  border: 1px solid var(--line);
  border-bottom-left-radius: 4px;
}

.msg-out {
  align-self: flex-end;
  background: #d4f1d9;
  border-bottom-right-radius: 4px;
}

.msg-meta {
  text-align: right;
  margin-top: .25rem;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: .2rem;
}

.msg-ora {
  font-size: .68rem;
  color: var(--muted);
  font-family: "JetBrains Mono", monospace;
}

.spunta {
  font-size: .7rem;
  color: var(--muted);
}

.spunta-letto { color: #2563eb; }
.spunta-fallito { color: var(--danger); }

/* ---- Input area ---- */
.chat-input {
  display: flex;
  gap: .5rem;
  padding: .65rem .85rem;
  border-top: 1px solid var(--line);
  background: var(--surface);
  flex-shrink: 0;
}

.chat-input input {
  flex: 1;
  height: 38px;
}

/* ---- Avvia template ---- */
#chat-avvia-wrap {
  padding: .6rem .85rem;
  border-top: 1px solid var(--line);
  background: var(--surface);
  flex-shrink: 0;
}

.chat-avvia-hint {
  font-size: .8rem;
  color: var(--muted);
  margin-bottom: .5rem;
}

.btn-avvia {
  background: var(--accent);
  color: #ffffff;
  border-radius: 999px;
  padding: 0 1.1rem;
  height: 36px;
  font-size: .85rem;
  font-weight: 600;
  transition: background 130ms ease;
}

.btn-avvia:hover { background: var(--accent-press); }

.btn-avvia em {
  font-style: normal;
  font-family: "JetBrains Mono", monospace;
  font-size: .8em;
  opacity: .85;
}

/* ================================================================
   LOGIN PAGE
   ================================================================ */

.login-body {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background: var(--sidebar);
  background-image: linear-gradient(160deg, #1a1d24 0%, #0f1116 100%);
  flex-direction: column;
}

.login-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 4px 8px rgba(0,0,0,.18), 0 24px 64px -16px rgba(0,0,0,.35);
  padding: 2.4rem 2rem 2rem;
  width: 100%;
  max-width: 340px;
  display: flex;
  flex-direction: column;
  gap: .85rem;
}

.login-logo {
  font-family: "Bricolage Grotesque", system-ui, sans-serif;
  font-weight: 700;
  font-size: 1.7rem;
  color: var(--ink);
  letter-spacing: -.03em;
  line-height: 1;
  margin-bottom: .15rem;
}

.login-sub {
  font-size: .84rem;
  color: var(--muted);
  margin-top: -.3rem;
  margin-bottom: .2rem;
}

.login-input {
  height: 44px;
  font-size: .9rem;
  border-radius: var(--radius-sm);
}

.login-btn {
  height: 44px;
  font-size: .9rem;
  background: var(--accent);
  color: #ffffff;
  border-radius: 999px;
  font-weight: 600;
  transition: background 130ms ease, opacity 130ms ease;
}

.login-btn:hover { background: var(--accent-press); }
.login-btn:disabled { opacity: .6; cursor: not-allowed; }

.login-err {
  font-size: .84rem;
  color: var(--danger);
  font-weight: 500;
  text-align: center;
  margin: 0;
}

/* ---- Logout link in sidebar footer ---- */
.sidebar-logout {
  display: block;
  font-size: .75rem;
  color: var(--sidebar-ink-soft);
  text-decoration: none;
  margin-top: .55rem;
  transition: color 120ms ease;
}

.sidebar-logout:hover { color: var(--sidebar-ink); }

/* ================================================================
   MOBILE — sidebar horizontal top bar (≤820px)
   ================================================================ */

@media (max-width: 820px) {
  body { flex-direction: column; }

  .sidebar {
    position: static;
    width: 100%;
    flex-direction: row;
    align-items: center;
    background: var(--sidebar);
    height: auto;
    padding: 0;
    overflow-x: auto;
  }

  .sidebar-wordmark {
    padding: .7rem 1rem;
    border-bottom: none;
    border-right: 1px solid rgba(255,255,255,.1);
    white-space: nowrap;
    font-size: 1rem;
    flex-shrink: 0;
  }

  nav {
    flex-direction: row;
    padding: .4rem .5rem;
    gap: 2px;
    flex: 1;
    overflow-x: auto;
    scrollbar-width: none;
  }

  nav::-webkit-scrollbar { display: none; }

  nav button {
    white-space: nowrap;
    width: auto;
    padding: .45rem .85rem;
    border-radius: 999px;
    flex-shrink: 0;
  }

  nav button.attivo::before { display: none; }

  nav button.attivo {
    background: rgba(79,70,229,.35);
  }

  .sidebar-footer { display: none; }

  .layout-content {
    margin-left: 0;
  }

  main {
    padding: 1.25rem 1rem;
  }

  .chat-wrap { height: 70vh; }

  .field-row {
    flex-direction: column;
    align-items: flex-start;
  }

  .field-row label { min-width: unset; }

  .field-row input,
  .field-row select {
    width: 100%;
  }
}
