:root{
  --bg:#f4f6f8; --card:#fff; --ink:#1a1f29; --muted:#6b7480; --line:#e3e8ee;
  --brand:#b760a8; --brand-d:#8a3f7f; --ok:#1f9d57; --warn:#c9881f; --err:#c0392b;
}
*{box-sizing:border-box}
body{margin:0;font-family:system-ui,-apple-system,Segoe UI,Roboto,sans-serif;background:var(--bg);color:var(--ink);font-size:15px;line-height:1.45}
a{color:var(--brand-d);text-decoration:none}
a:hover{text-decoration:underline}
h1{font-size:1.5rem;margin:.2rem 0 1rem}
h2{font-size:1.1rem;margin:0 0 .8rem}
.muted{color:var(--muted)}
.small{font-size:.83rem}

.topbar{display:flex;align-items:center;gap:1.5rem;background:var(--card);border-bottom:1px solid var(--line);padding:.7rem 1.3rem}
.topbar .brand a{font-weight:700;color:var(--ink)}
.topbar nav{display:flex;gap:1rem;flex:1}
.topbar .user{color:var(--muted)}

main.app{max-width:1080px;margin:1.5rem auto;padding:0 1.2rem}
main.auth{min-height:100vh;display:flex;flex-direction:column;align-items:center;justify-content:center;gap:1rem;padding:1rem}
main.auth .flash{width:100%;max-width:380px;margin:0}

.card{background:var(--card);border:1px solid var(--line);border-radius:12px;padding:1.2rem 1.4rem;box-shadow:0 1px 3px rgba(0,0,0,.04);margin-bottom:1.2rem}
.auth-card{width:100%;max-width:380px}
.cards{display:grid;grid-template-columns:repeat(auto-fit,minmax(180px,1fr));gap:1rem;margin-bottom:1.2rem}
.stat{text-align:center}
.stat-num{font-size:2rem;font-weight:700;color:var(--brand-d)}
.stat-lbl{font-weight:600}

label{display:block;font-weight:600;font-size:.85rem;margin:.7rem 0 .25rem}
input,select,textarea{width:100%;padding:.55rem .7rem;border:1px solid #cfd6dd;border-radius:8px;font:inherit;background:#fff}
input:focus,select:focus,textarea:focus{outline:0;border-color:var(--brand);box-shadow:0 0 0 3px rgba(183,96,168,.18)}
.btn{display:inline-block;cursor:pointer;border:1px solid var(--line);background:#fff;color:var(--ink);padding:.5rem .9rem;border-radius:8px;font:inherit;font-weight:600}
.btn:hover{text-decoration:none;background:#f0f3f6}
.btn-primary{background:var(--brand);border-color:var(--brand);color:#fff;width:100%;margin-top:1rem}
.btn-primary:hover{background:var(--brand-d)}

.flash{padding:.7rem .9rem;border-radius:8px;margin-bottom:1rem;font-size:.9rem}
.flash-info{background:#e3f1f4;color:#155e69;border:1px solid #bfe2e8}
.flash-error{background:#fbe5e2;color:#922;border:1px solid #f2c4bd}


/* ========== gaia_social — pannello threads (Step E) ========== */
/* Da appendere a /assets/style.css esistente.  */

.filters-bar {
    display: flex;
    flex-wrap: wrap;
    gap: .8rem;
    align-items: center;
    margin: 1rem 0;
    padding: .8rem 1rem;
    background: #f7f7f9;
    border-radius: 8px;
}
.filters-bar .filter-toggle { font-size: .9rem; }
.filters-bar select { padding: .3rem .5rem; }

.threads-table {
    width: 100%;
    border-collapse: collapse;
    margin-top: .8rem;
    background: #fff;
}
.threads-table th, .threads-table td {
    padding: .65rem .8rem;
    border-bottom: 1px solid #eee;
    text-align: left;
    font-size: .92rem;
}
.threads-table th { background: #fafafa; font-weight: 600; }
.threads-table tr.thread-row { cursor: pointer; transition: background .15s; }
.threads-table tr.thread-row:hover { background: #fafbfc; }
.threads-table tr.thread-row.has-pending { background: #fff8e1; }
.threads-table tr.thread-row.has-pending:hover { background: #fff3c4; }
.threads-table td.center, .threads-table th.center { text-align: center; }
.threads-table td.ts { white-space: nowrap; font-variant-numeric: tabular-nums; }
.threads-table td.preview { color: #555; }
.threads-table td.platform { font-size: .85rem; color: #777; }

.badge {
    display: inline-block;
    padding: .15rem .55rem;
    border-radius: 12px;
    font-size: .78rem;
    font-weight: 600;
    background: #eee;
}
.badge-pending  { background: #ffd54f; color: #5d4037; }
.badge-protect  { background: #ffe0e0; color: #b71c1c; }
.phase          { font-size: .8rem; color: #555; }
.phase-discovery_iniziale   { color: #1565c0; }
.phase-ascolto_supporto     { color: #2e7d32; }
.phase-confidenza_consolidata { color: #6a1b9a; }
.phase-commerciale_soft     { color: #e65100; }
.phase-commerciale_attivo   { color: #b71c1c; font-weight: 600; }
.mood           { font-size: .8rem; }
.mood-crisi      { color: #b71c1c; font-weight: 600; }
.mood-fragilita  { color: #ef6c00; }
.mood-neutro     { color: #777; }
.mood-curiosita  { color: #1565c0; }
.mood-energia_positiva { color: #2e7d32; }

/* ----- Thread detail / chat ----- */
.thread-header { margin-bottom: 1rem; }
.thread-header .back-link {
    display: inline-block; font-size: .9rem; color: #555;
    margin-bottom: .5rem; text-decoration: none;
}
.thread-header .back-link:hover { color: #000; }
.thread-meta { margin-bottom: .8rem; }
.inline-form { display: inline-block; margin-left: .5rem; }

70vh;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: .5rem;
    margin-bottom: 1.5rem;
}
.bubble {
    max-width: 75%;
    padding: .55rem .8rem;
    border-radius: 14px;
    font-size: .95rem;
    line-height: 1.35;
}
.bubble-user {
    background: #fff;
    align-self: flex-start;
    border: 1px solid #e0e0e0;
}
.bubble-gaia {
    background: #dcf8c6;
    align-self: flex-end;
}
.bubble-text { word-wrap: break-word; }
.bubble-meta {
    font-size: .7rem;
    color: #999;
    margin-top: .25rem;
    text-align: right;
}

/* ----- Bozze pending ----- */
.bozza-card {
    background: #fff8e1;
    border: 1px solid #f6c84c;
    border-radius: 10px;
    padding: 1rem;
    margin-bottom: 1rem;
}
.bozza-meta { margin-bottom: .6rem; }
.bozza-textarea {
    width: 100%;
    padding: .6rem .8rem;
    border: 1px solid #ddd;
    border-radius: 8px;
    font-family: inherit;
    font-size: .95rem;
    resize: vertical;
    background: #fff;
}
.bozza-actions {
    display: flex;
    gap: .5rem;
    margin-top: .8rem;
    flex-wrap: wrap;
}
.bozza-empty {
    background: #fafafa;
    border: 1px dashed #ddd;
    border-radius: 10px;
    padding: 1.5rem;
    text-align: center;
}

/* ----- Bottoni generici (se mancanti nello style.css originale) ----- */
.btn {
    display: inline-block;
    padding: .45rem .9rem;
    border-radius: 6px;
    border: 1px solid #ddd;
    background: #fff;
    cursor: pointer;
    font-size: .9rem;
    text-decoration: none;
    color: #333;
}
.btn:hover { background: #f5f5f5; }
.btn-primary { background: #1565c0; color: #fff; border-color: #1565c0; }
.btn-primary:hover { background: #0d47a1; }
.btn-ghost { background: transparent; color: #666; }
.btn-small { padding: .25rem .55rem; font-size: .82rem; }

/* ========== Step E.1 — sidebar contesto operatore ========== */

/* Thread header con avatar */
.thread-title-row {
    display: flex;
    align-items: center;
    gap: .9rem;
    margin: .3rem 0 .6rem 0;
}
.thread-title-row h1 {
    margin: 0;
    font-size: 1.55rem;
    line-height: 1.2;
}
.thread-avatar {
    width: 56px;
    height: 56px;
    border-radius: 50%;
    object-fit: cover;
    flex-shrink: 0;
    border: 1px solid #e5e5e5;
    background: #f5f5f5;
}
.thread-avatar-placeholder {
    display: flex;
    align-items: center;
    justify-content: center;
    background: #cfd8dc;
    color: #fff;
    font-size: 1.4rem;
    font-weight: 600;
}

.thread-layout {
    display: grid;
    grid-template-columns: 1fr 340px;
    gap: 1.5rem;
    margin-top: 1rem;
}
@media (max-width: 900px) {
    .thread-layout { grid-template-columns: 1fr; }
}

.thread-main { min-width: 0; }

.thread-sidebar {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.sidebar-section {
    background: #fff;
    border: 1px solid #eee;
    border-radius: 10px;
    padding: 1rem;
}
.sidebar-section h3 {
    margin: 0 0 .7rem 0;
    font-size: .95rem;
    font-weight: 600;
    color: #444;
    padding-bottom: .4rem;
    border-bottom: 1px solid #f0f0f0;
}
.sidebar-section .kv {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    padding: .35rem 0;
    font-size: .88rem;
    border-bottom: 1px dashed #f5f5f5;
}
.sidebar-section .kv:last-child { border-bottom: none; }
.sidebar-section .kv .k { color: #777; }
.sidebar-section .kv .v { color: #222; }

.sidebar-form label {
    display: block;
    font-size: .82rem;
    color: #555;
    margin: .55rem 0 .15rem 0;
    font-weight: 500;
}
.sidebar-form label.flex-row {
    display: flex !important;
    flex-direction: row;
    align-items: center;
    justify-content: flex-start;
    gap: .55rem;
    margin: .9rem 0 0 0;
    font-weight: 500;
    color: #333;
    cursor: pointer;
}
.sidebar-form label.flex-row input[type="checkbox"] {
    margin: 0;
    flex-shrink: 0;
    width: 16px;
    height: 16px;
    cursor: pointer;
}
.sidebar-form select,
.sidebar-form input[type="number"] {
    width: 100%;
    padding: .35rem .55rem;
    border: 1px solid #ddd;
    border-radius: 6px;
    font-size: .9rem;
    background: #fff;
}
.sidebar-form input[type="number"] { max-width: 90px; }
.sidebar-textarea {
    width: 100%;
    padding: .55rem;
    border: 1px solid #ddd;
    border-radius: 6px;
    font-family: inherit;
    font-size: .88rem;
    resize: vertical;
    background: #fff;
}

.btn-block { display: block; width: 100%; text-align: center; margin-top: .8rem; }
.xsmall { font-size: .75rem; }

.meta-chip {
    display: inline-block;
    padding: .15rem .55rem;
    background: #f3f4f6;
    border-radius: 10px;
    font-size: .78rem;
    margin-right: .3rem;
    margin-bottom: .2rem;
}

.gaia-state-mini { background: #fff7e6; border-color: #ffe4b3; }

/* Chat-info: barra in alto sopra la chat-window con count + indicazione scroll */
.chat-info {
    padding: .45rem .8rem;
    background: #fff;
    border: 1px solid #e5e5e5;
    border-radius: 8px;
    margin-bottom: .4rem;
    color: #555;
}
.chat-info .arrow-up {
    display: inline-block;
    padding: 0 .3rem;
    background: #e3f2fd;
    border-radius: 3px;
    color: #1565c0;
    font-weight: 600;
}

/* .chat-window: ridefinito completo (sovrascrive il blocco di Step E
   perché in CSS l'ultimo wins quando stessa specificità). */
.chat-window {
    background: #f0f2f5;
    padding: 1rem;
    border-radius: 12px;
    max-height: 70vh;
    overflow-y: scroll;
    display: flex;
    flex-direction: column;
    gap: .5rem;
    margin-bottom: 1.5rem;
    scroll-behavior: smooth;
}
.chat-window::-webkit-scrollbar { width: 10px; }
.chat-window::-webkit-scrollbar-thumb {
    background: #cbd5e1; border-radius: 5px;
}
.chat-window::-webkit-scrollbar-thumb:hover { background: #94a3b8; }
.chat-window::-webkit-scrollbar-track {
    background: #e2e8f0; border-radius: 5px;
}

/* Override esplicito bubble per garantire rendering pillole */
.chat-window .bubble {
    border-radius: 18px !important;
    max-width: 75% !important;
    padding: .6rem .9rem !important;
}
.chat-window .bubble-user {
    align-self: flex-start !important;
    background: #ffffff !important;
    border: 1px solid #e0e0e0 !important;
}
.chat-window .bubble-gaia {
    align-self: flex-end !important;
    background: #dcf8c6 !important;
    border: none !important;
}

/* ===== Step H.4 — bubble media (image / audio / video) ===== */
.chat-window .bubble-with-media {
  padding: 6px 8px 8px;
  background: rgba(15, 23, 42, 0.04);
}
.chat-window .bubble-user.bubble-with-media {
  background: rgba(15, 23, 42, 0.04);
}
.chat-window .bubble-gaia.bubble-with-media {
  background: rgba(236, 72, 153, 0.06);
}
.bubble-media-link {
  display: block;
  line-height: 0;
  border-radius: 8px;
  overflow: hidden;
}
.bubble-media-img {
  display: block;
  max-width: 280px;
  max-height: 360px;
  width: auto;
  height: auto;
  border-radius: 8px;
  object-fit: cover;
  cursor: zoom-in;
}
.bubble-media-audio {
  display: block;
  width: 100%;
  max-width: 280px;
  margin: 2px 0;
}
.bubble-media-video {
  display: block;
  max-width: 320px;
  max-height: 400px;
  border-radius: 8px;
}
.bubble-media-fallback {
  display: inline-block;
  padding: 6px 10px;
  background: #e2e8f0;
  border-radius: 6px;
  text-decoration: none;
  color: #334155;
  font-size: 13px;
}
.bubble-caption-ai {
  margin-top: 6px;
  padding: 5px 8px;
  font-size: 11px;
  font-style: italic;
  color: #475569;
  background: rgba(255, 255, 255, 0.72);
  border-radius: 4px;
  border-left: 2px solid #94a3b8;
  line-height: 1.4;
  max-width: 280px;
}
.bubble-caption-ai .caption-icon { opacity: 0.75; margin-right: 2px; }
.bubble-media-tags { margin-top: 4px; }
.meta-chip-gps {
  display: inline-block;
  font-size: 11px;
  padding: 2px 6px;
  background: #fef3c7;
  color: #92400e;
  border-radius: 4px;
}
.bubble-with-media .bubble-text {
  margin-top: 6px;
  font-size: 13px;
}

/* ===== Step H.4 — lightbox media (dialog HTML5) ===== */
.bubble-media-link {
  display: block;
  padding: 0;
  border: none;
  background: transparent;
  cursor: zoom-in;
  border-radius: 8px;
  overflow: hidden;
}
.bubble-media-link:focus-visible {
  outline: 2px solid #6366f1;
  outline-offset: 2px;
}
.media-lightbox {
  border: none;
  padding: 0;
  background: transparent;
  max-width: 95vw;
  max-height: 95vh;
  margin: auto;
}
.media-lightbox::backdrop {
  background: rgba(0, 0, 0, 0.82);
}
.media-lightbox-img {
  display: block;
  max-width: 95vw;
  max-height: 85vh;
  width: auto;
  height: auto;
  border-radius: 6px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
}
.media-lightbox-close {
  position: fixed;
  top: 16px;
  right: 16px;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: none;
  background: rgba(255, 255, 255, 0.92);
  color: #1f2937;
  font-size: 24px;
  line-height: 1;
  cursor: pointer;
  z-index: 10;
}
.media-lightbox-close:hover {
  background: #fff;
}
.media-lightbox-cap {
  margin-top: 12px;
  padding: 8px 12px;
  background: rgba(255, 255, 255, 0.92);
  border-radius: 6px;
  color: #1f2937;
  font-size: 13px;
  font-style: italic;
  text-align: center;
  max-width: 95vw;
}

/* ===== Step H — bozza auto_no_reply (LLM ha rifiutato di rispondere) ===== */
.bozza-card-noreply {
  border: 2px solid #dc2626;
  background: #fef2f2;
}
.noreply-tag {
  color: #991b1b;
  background: #fee2e2;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 11px;
  letter-spacing: 0.5px;
}
.noreply-explain {
  margin: 8px 0;
  padding: 8px 10px;
  background: #fff;
  border-left: 3px solid #dc2626;
  color: #7f1d1d;
  font-size: 13px;
  line-height: 1.5;
  border-radius: 4px;
}
.noreply-text {
  margin: 8px 0;
  padding: 8px 10px;
  background: #f3f4f6;
  color: #4b5563;
  font-family: ui-monospace, monospace;
  font-size: 12px;
  border-radius: 4px;
  font-style: italic;
}

/* ===== Voice draft (Sonnet ha emesso [VOICE]) ===== */
.bozza-card-voice {
  border: 2px solid #6366f1;
  background: #eef2ff;
}
.voice-tag {
  color: #4338ca;
  background: #c7d2fe;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 11px;
  letter-spacing: 0.5px;
}
.voice-pending-info {
  margin: 8px 0;
  padding: 8px 10px;
  background: #fff;
  border-left: 3px solid #6366f1;
  color: #3730a3;
  font-size: 13px;
  border-radius: 4px;
}
.bozza-audio-preview {
  width: 100%;
  margin: 8px 0;
}
