/* Ygon — UI web. Stile SaaS "data-dense dashboard": Inter, palette blu/slate,
   superfici chiare, tabelle nitide. Tutto self-hosted (CSP self-only): nessun
   CDN, nessun font esterno, nessuno stile inline. */

/* ---- Font auto-ospitato (variabile, un file per tutti i pesi) ---- */
@font-face {
    font-family: 'Inter';
    font-style: normal;
    font-weight: 100 900;
    font-display: swap;
    src: url('/assets/fonts/inter-variable.woff2') format('woff2');
}

/* ---- Design tokens ---- */
:root {
    --font: 'Inter', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;

    --primary: #2563eb;
    --primary-hover: #1d4ed8;
    --on-primary: #ffffff;
    --accent: #059669;

    --bg: #eef2fb;
    --surface: #ffffff;
    --surface-muted: #f8fafc;
    --fg: #0f172a;
    --muted-fg: #64748b;

    --muted: #f1f5f9;
    --border: #e2e8f0;
    --border-strong: #cbd5e1;

    --danger: #dc2626;
    --danger-hover: #b91c1c;
    --danger-bg: #fef2f2;
    --danger-border: #fecaca;

    --info-bg: #eff6ff;
    --info-border: #bfdbfe;
    --info-fg: #1e40af;

    --warn-bg: #fffbeb;
    --warn-border: #fde68a;
    --warn-fg: #92400e;

    --ring: rgba(37, 99, 235, 0.35);

    --radius: 10px;
    --radius-sm: 8px;
    --radius-lg: 14px;

    --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.06), 0 1px 3px rgba(15, 23, 42, 0.04);
    --shadow: 0 4px 16px rgba(15, 23, 42, 0.08);
}

/* ---- Reset & base ---- */
*, *::before, *::after { box-sizing: border-box; }

body {
    margin: 0;
    font-family: var(--font);
    font-size: 15px;
    line-height: 1.55;
    color: var(--fg);
    background: var(--bg);
    -webkit-font-smoothing: antialiased;
    text-rendering: optimizeLegibility;
}

main {
    max-width: 68rem;
    margin: 0 auto;
    padding: 1.75rem 1.5rem 3rem;
}

h1, h2, h3 { line-height: 1.25; font-weight: 600; letter-spacing: -0.01em; }
h1 { font-size: 1.4rem; margin: 0 0 1.25rem; }
h2 {
    font-size: 1.05rem;
    margin: 1.75rem 0 0.75rem;
    color: var(--fg);
    padding-left: 0.6rem;
    border-left: 3px solid var(--primary);
}
h3 { font-size: 0.95rem; }

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
img { max-width: 100%; }

/* ---- Top nav (barra SaaS) ---- */
.top-nav {
    display: flex;
    align-items: center;
    gap: 0.35rem;
    padding: 0.65rem 1.5rem;
    background: linear-gradient(90deg, #2563eb 0%, #4f46e5 55%, #7c3aed 100%);
    border-bottom: 1px solid rgba(255, 255, 255, 0.12);
    position: sticky;
    top: 0;
    z-index: 40;
    box-shadow: 0 2px 10px rgba(37, 99, 235, 0.3);
}

.top-nav a {
    color: rgba(255, 255, 255, 0.85);
    text-decoration: none;
    font-weight: 500;
    font-size: 0.9rem;
    padding: 0.4rem 0.7rem;
    border-radius: var(--radius-sm);
    transition: background 0.15s ease, color 0.15s ease;
}
.top-nav a:hover { background: rgba(255, 255, 255, 0.16); color: #fff; text-decoration: none; }

/* Il primo link è il brand */
.top-nav a:first-child {
    color: #fff;
    font-weight: 700;
    font-size: 1.05rem;
    letter-spacing: -0.02em;
    margin-right: 0.5rem;
}
.top-nav a:first-child::before {
    content: "";
    display: inline-block;
    width: 0.55rem;
    height: 0.55rem;
    margin-right: 0.5rem;
    border-radius: 50%;
    background: #fff;
    box-shadow: 0 0 0 3px rgba(255, 255, 255, 0.25);
    vertical-align: middle;
}

.logout-form { margin-left: auto; }
/* "Esci" sulla barra colorata: contorno chiaro traslucido */
.top-nav .logout-form button {
    color: #fff;
    background: rgba(255, 255, 255, 0.12);
    border-color: rgba(255, 255, 255, 0.45);
}
.top-nav .logout-form button:hover {
    background: rgba(255, 255, 255, 0.22);
    border-color: rgba(255, 255, 255, 0.7);
}

/* ---- Superfici / card ---- */
.filters, .add-fact, .mood-display, .silence-display {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow-sm);
}

.filters, .add-fact {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-end;
    gap: 0.9rem;
    padding: 1rem;
    margin-bottom: 1.25rem;
}

.mood-display, .silence-display { padding: 0.85rem 1rem; margin-bottom: 1rem; }
.mood-display p, .silence-display p { margin: 0; }

.filters label, .add-fact label {
    display: flex;
    flex-direction: column;
    gap: 0.3rem;
    font-size: 0.75rem;
    font-weight: 600;
    letter-spacing: 0.02em;
    text-transform: uppercase;
    color: var(--muted-fg);
}

/* checkbox/radio inline: niente colonna, niente maiuscolo */
.filters label:has(input[type="checkbox"]) {
    flex-direction: row;
    align-items: center;
    gap: 0.4rem;
    text-transform: none;
    font-size: 0.85rem;
    font-weight: 500;
    color: var(--fg);
    align-self: center;
}

/* ---- Form controls ---- */
input[type="text"], input[type="password"], input[type="search"],
select, textarea {
    font: inherit;
    color: var(--fg);
    background: var(--surface);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
    padding: 0.5rem 0.65rem;
    min-height: 2.4rem;
    transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
input[type="text"]:focus, input[type="password"]:focus, input[type="search"]:focus,
select:focus, textarea:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--ring);
}
input::placeholder, textarea::placeholder { color: #94a3b8; }
select { cursor: pointer; }

/* ---- Bottoni ---- */
button {
    font: inherit;
    font-weight: 600;
    font-size: 0.875rem;
    color: var(--on-primary);
    background: var(--primary);
    border: 1px solid var(--primary);
    border-radius: var(--radius-sm);
    padding: 0.5rem 0.95rem;
    min-height: 2.4rem;
    cursor: pointer;
    transition: background 0.15s ease, border-color 0.15s ease, box-shadow 0.15s ease;
    white-space: nowrap;
}
button:hover { background: var(--primary-hover); border-color: var(--primary-hover); }
button:focus-visible { outline: none; box-shadow: 0 0 0 3px var(--ring); }
button:active { transform: translateY(0.5px); }

/* Azioni secondarie: neutre (bordo, sfondo chiaro) */
.logout-form button,
.personality-reload button,
form[action$="/toggle"] button,
.facts-table .col-content button {
    color: var(--fg);
    background: var(--surface);
    border-color: var(--border-strong);
}
.logout-form button:hover,
.personality-reload button:hover,
form[action$="/toggle"] button:hover,
.facts-table .col-content button:hover {
    background: var(--muted);
    border-color: var(--border-strong);
}

/* Azioni distruttive: rosse */
form[action$="/delete"] button,
.delete-form button {
    color: var(--danger);
    background: var(--surface);
    border-color: var(--danger-border);
}
form[action$="/delete"] button:hover,
.delete-form button:hover {
    color: var(--on-primary);
    background: var(--danger);
    border-color: var(--danger);
}

/* "Aggiungi" un fatto: verde (creazione = azione positiva) */
.add-fact button { background: var(--accent); border-color: var(--accent); }
.add-fact button:hover { background: #047857; border-color: #047857; }

/* Link "Azzera" accanto ai filtri: aspetto da bottone ghost */
.filters a {
    align-self: center;
    font-size: 0.875rem;
    font-weight: 500;
    color: var(--muted-fg);
    padding: 0.45rem 0.6rem;
    border-radius: var(--radius-sm);
}
.filters a:hover { background: var(--muted); color: var(--fg); text-decoration: none; }

/* ---- Tabelle (data-dense) ---- */
.facts-table, .conversations-table, .reminders-table,
.proactive-log-table, .open-threads-table {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
    box-shadow: var(--shadow-sm);
    font-variant-numeric: tabular-nums;
    margin-bottom: 1.25rem;
}

.facts-table th, .conversations-table th, .reminders-table th,
.proactive-log-table th, .open-threads-table th {
    text-align: left;
    font-size: 0.72rem;
    font-weight: 600;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: var(--info-fg);
    background: var(--info-bg);
    padding: 0.65rem 0.9rem;
    border-bottom: 1px solid var(--info-border);
}

.facts-table td, .conversations-table td, .reminders-table td,
.proactive-log-table td, .open-threads-table td {
    padding: 0.7rem 0.9rem;
    border-bottom: 1px solid var(--border);
    vertical-align: top;
}
/* niente bordo sotto l'ultima riga (angoli tondi puliti) */
.facts-table tbody tr:last-child td,
.conversations-table tbody tr:last-child td,
.reminders-table tbody tr:last-child td,
.proactive-log-table tbody tr:last-child td,
.open-threads-table tbody tr:last-child td { border-bottom: none; }

/* hover riga */
.facts-table tbody tr:hover,
.conversations-table tbody tr:hover,
.reminders-table tbody tr:hover,
.proactive-log-table tbody tr:hover,
.open-threads-table tbody tr:hover { background: #f0f6ff; }

/* riga fatto disattivato / thread chiuso */
.facts-table tr.inactive { opacity: 0.6; }
.open-threads-table tr.thread-closed { opacity: 0.55; }

/* Follow-up evitato: attività reale, ma nessun messaggio inviato. */
.proactive-log-table tr.log-skipped { opacity: 0.65; font-style: italic; }

.hint { color: var(--muted-fg); font-size: 0.85rem; margin-top: 0.6rem; }

/* colonna Stato: verde per Attivo, muto per Disattivato */
.facts-table tr:not(.inactive) td:nth-child(4) { color: var(--accent); font-weight: 600; }
.facts-table tr.inactive td:nth-child(4) { color: var(--muted-fg); }

/* messaggio: chi (prima colonna) in evidenza tenue */
.conversations-table tr.role-assistant td:first-child { color: var(--primary); font-weight: 600; }
.conversations-table tr.role-user td:first-child { color: var(--fg); font-weight: 600; }

/* form di modifica inline nella cella contenuto */
.facts-table .col-content form {
    display: flex;
    flex-wrap: wrap;
    gap: 0.45rem;
    align-items: center;
}
.facts-table .col-content input[type="text"] { flex: 1; min-width: 14rem; }
.facts-table .col-actions { white-space: nowrap; }
.facts-table .col-actions form { display: inline-block; margin-right: 0.35rem; }

/* Fatti: leggibili di default, modificabili con la matita.
   Il form di modifica resta nel DOM ma nascosto finché la riga non ha .editing
   (toggle client-side in app.js; dopo il salvataggio htmx ri-renderizza la riga
   senza .editing, quindi torna da sola in lettura). */
.facts-table .col-content .fact-read {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 0.75rem;
}
.facts-table .col-content .fact-text {
    display: flex;
    flex-direction: column;
    gap: 0.15rem;
    min-width: 0;
}
.facts-table .col-content .fact-content { color: var(--fg); word-break: break-word; }
.facts-table .col-content .fact-category {
    font-size: 0.72rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--muted-fg);
}
/* Matita: sola icona, discreta finché non ci passi sopra. */
.facts-table .col-content button.fact-edit-toggle {
    flex: none;
    padding: 0.3rem;
    line-height: 0;
    color: var(--muted-fg);
    background: transparent;
    border-color: transparent;
    box-shadow: none;
}
.facts-table .col-content button.fact-edit-toggle:hover {
    color: var(--primary);
    background: var(--muted);
    border-color: transparent;
}
/* Lettura di default, modifica solo su .editing. */
.facts-table .col-content .fact-edit { display: none; }
.facts-table tr.editing .col-content .fact-read { display: none; }
.facts-table tr.editing .col-content .fact-edit { display: flex; }

/* Impostazioni: ogni opzione su una riga propria, checkbox e testo allineati
   in alto così il testo lungo va a capo senza scavalcare la casella. */
.settings-form {
    display: flex;
    flex-direction: column;
    gap: 0.85rem;
    align-items: flex-start;
}
.settings-form label {
    display: flex;
    align-items: flex-start;
    gap: 0.55rem;
    line-height: 1.4;
    font-weight: 500;
}
.settings-form label input[type="checkbox"] { flex: none; margin-top: 0.2rem; }

/* ---- Fieldset modalità ricerca ---- */
.mode-choice {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    border: none;
    padding: 0;
    margin: 0;
}
.mode-choice legend {
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.02em;
    color: var(--muted-fg);
    padding: 0;
}
.mode-choice label {
    display: flex;
    align-items: center;
    gap: 0.35rem;
    font-weight: 500;
    font-size: 0.9rem;
}

/* ---- Banner / stati ---- */
.notice {
    color: var(--info-fg);
    background: var(--info-bg);
    border: 1px solid var(--info-border);
    border-radius: var(--radius-sm);
    padding: 0.65rem 0.9rem;
    margin-bottom: 1rem;
    font-size: 0.9rem;
}
.notice.error {
    color: var(--warn-fg);
    background: var(--warn-bg);
    border-color: var(--warn-border);
}
.error { color: var(--danger); font-weight: 500; font-size: 0.9rem; }

/* celle "Nessun..." ed empty state */
.facts-table td[colspan], .conversations-table td[colspan],
.reminders-table td[colspan], .proactive-log-table td[colspan],
.open-threads-table td[colspan],
.none { color: var(--muted-fg); font-style: italic; }

/* ---- Personalità ---- */
.personality-form {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    margin-bottom: 1rem;
}
.personality-form textarea {
    width: 100%;
    min-height: 24rem;
    line-height: 1.6;
    resize: vertical;
}
.personality-actions { display: flex; gap: 0.75rem; align-items: center; }
.personality-reload { display: inline; }

/* ---- Proattivo ---- */
.proactive-section { margin-bottom: 1.75rem; }

/* ---- Paginazione ---- */
.pagination {
    display: flex;
    align-items: center;
    gap: 1rem;
    margin-top: 1rem;
    font-size: 0.9rem;
    color: var(--muted-fg);
}
.pagination a {
    padding: 0.4rem 0.7rem;
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
    color: var(--fg);
    font-weight: 500;
}
.pagination a:hover { background: var(--muted); text-decoration: none; }

/* ---- Login (pagina senza top-nav): card centrata ---- */
body:not(:has(.top-nav)) {
    display: grid;
    place-items: center;
    min-height: 100dvh;
    padding: 1.5rem;
}
body:not(:has(.top-nav)) main {
    width: 100%;
    max-width: 22rem;
    padding: 2rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-top: 4px solid var(--primary);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow);
}
body:not(:has(.top-nav)) h1 {
    text-align: center;
    margin-bottom: 1.5rem;
}
body:not(:has(.top-nav)) form { display: flex; flex-direction: column; gap: 0.75rem; }
body:not(:has(.top-nav)) label {
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--muted-fg);
}
body:not(:has(.top-nav)) input[type="password"] { width: 100%; }
body:not(:has(.top-nav)) button { width: 100%; margin-top: 0.25rem; }

/* ---- Collezioni ----
   Metafora del quaderno, dentro lo stesso sistema (Inter, blu/slate, card):
   le collezioni sono linguette in alto, ogni voce è una nota con il filo di
   margine a sinistra. Nessun colore o font nuovo, solo i token esistenti. */

/* Linguette: chip a pillola, selezionata = piena, archiviata = barrata */
.collection-tabs {
    display: flex;
    flex-wrap: wrap;
    gap: 0.4rem;
    margin-bottom: 1.5rem;
    padding-bottom: 0.9rem;
    border-bottom: 1px solid var(--border);
}
.collection-tab {
    display: inline-flex;
    align-items: center;
    gap: 0.45rem;
    padding: 0.4rem 0.85rem;
    border-radius: 999px;
    font-size: 0.9rem;
    font-weight: 500;
    color: var(--muted-fg);
    background: var(--surface);
    border: 1px solid var(--border);
    box-shadow: var(--shadow-sm);
    transition: color 0.15s ease, border-color 0.15s ease, background 0.15s ease;
}
.collection-tab:hover { color: var(--fg); border-color: var(--border-strong); text-decoration: none; }
.collection-tab.is-active { color: var(--on-primary); background: var(--primary); border-color: var(--primary); }
.collection-tab.is-active:hover { color: var(--on-primary); background: var(--primary-hover); }
.collection-tab.is-closed .tab-name { text-decoration: line-through; opacity: 0.85; }
.collection-tab .tab-flag {
    font-size: 0.64rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    padding: 0.05rem 0.4rem;
    border-radius: 999px;
    background: var(--muted);
    color: var(--muted-fg);
}
.collection-tab.is-active .tab-flag { background: rgba(255, 255, 255, 0.22); color: #fff; }

/* Pannello della collezione aperta */
.collection-panel {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-sm);
    padding: 1.25rem 1.35rem 1.35rem;
}
.collection-head {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    margin-bottom: 1.1rem;
    padding-bottom: 1.1rem;
    border-bottom: 1px solid var(--border);
}
/* Il nome è un titolo editabile: discreto finché non lo tocchi */
.rename-form { display: flex; align-items: center; gap: 0.5rem; flex: 1; min-width: 12rem; }
.rename-form input[type="text"] {
    flex: 1;
    min-width: 0;
    min-height: 2.4rem;
    font-size: 1.15rem;
    font-weight: 600;
    letter-spacing: -0.01em;
    color: var(--fg);
    background: transparent;
    border: 1px solid transparent;
}
.rename-form input[type="text"]:hover { border-color: var(--border); }
.rename-form input[type="text"]:focus { border-color: var(--primary); background: var(--surface); }
.rename-form button { color: var(--fg); background: var(--surface); border-color: var(--border-strong); }
.rename-form button:hover { background: var(--muted); border-color: var(--border-strong); }

/* Archivia / Riapri: azione secondaria, contorno neutro */
.close-form button.ghost { color: var(--muted-fg); background: transparent; border-color: var(--border-strong); }
.close-form button.ghost:hover { color: var(--fg); background: var(--muted); border-color: var(--border-strong); }

.collection-note {
    margin: 0 0 1.1rem;
    padding: 0.6rem 0.8rem;
    font-size: 0.85rem;
    color: var(--warn-fg);
    background: var(--warn-bg);
    border: 1px solid var(--warn-border);
    border-radius: var(--radius-sm);
}

/* Voci: ogni nota è carta con il filo di margine del quaderno a sinistra */
.entry-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 0.7rem; }
.entry {
    display: grid;
    grid-template-columns: 1fr auto;
    align-items: start;
    gap: 0.15rem 0.75rem;
    padding: 0.8rem 0.9rem 0.85rem 1.05rem;
    background: var(--surface-muted);
    border: 1px solid var(--border);
    border-left: 3px solid var(--primary);
    border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
}
.entry-date {
    grid-column: 1;
    font-size: 0.7rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--muted-fg);
    font-variant-numeric: tabular-nums;
}
.entry-content { grid-column: 1; margin: 0.1rem 0 0; color: var(--fg); line-height: 1.55; overflow-wrap: anywhere; }
.entry-delete { grid-column: 2; grid-row: 1 / span 2; align-self: center; margin: 0; }
.entry-delete button {
    min-height: auto;
    padding: 0.25rem 0.45rem;
    line-height: 1;
    font-size: 0.9rem;
    color: var(--muted-fg);
    background: transparent;
    border-color: transparent;
    box-shadow: none;
    opacity: 0;
    transition: opacity 0.15s ease, color 0.15s ease, background 0.15s ease;
}
.entry:hover .entry-delete button, .entry-delete button:focus-visible { opacity: 1; }
.entry-delete button:hover { color: var(--on-primary); background: var(--danger); border-color: var(--danger); }
/* Su touch (niente hover) la ✕ è sempre visibile, altrimenti sarebbe irraggiungibile */
@media (hover: none) { .entry-delete button { opacity: 1; } }

/* Stati vuoti e guide */
.collections-empty {
    background: var(--surface);
    border: 1px dashed var(--border-strong);
    border-radius: var(--radius-lg);
    padding: 2.25rem 1.5rem;
    text-align: center;
}
.collections-empty .empty-title { font-size: 1.05rem; font-weight: 600; color: var(--fg); margin: 0 0 0.4rem; }
.collections-empty .empty-hint { margin: 0 auto; max-width: 34rem; color: var(--muted-fg); }
.entry-empty { padding: 1rem; font-style: italic; color: var(--muted-fg); list-style: none; }
.collection-hint { padding: 0.4rem 0; color: var(--muted-fg); }

/* ---- Accessibilità ---- */
:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }

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

/* ---- Responsive ---- */
@media (max-width: 640px) {
    main { padding: 1.25rem 1rem 2.5rem; }
    .top-nav { flex-wrap: wrap; padding: 0.6rem 1rem; }
    .filters, .add-fact { padding: 0.85rem; }
    .facts-table th, .conversations-table th, .reminders-table th,
    .proactive-log-table th, .open-threads-table th,
    .facts-table td, .conversations-table td, .reminders-table td,
    .proactive-log-table td, .open-threads-table td { padding: 0.55rem 0.6rem; }
}
