/* Farmabooster v2 — analytics
   Nessun framework: i token stanno qui, il resto e' layout. */

:root {
    --bg:            #0f1115;
    --surface:       #171a21;
    --surface-2:     #1e222b;
    --line:          #2a2f3a;
    --text:          #e8eaf0;
    --text-dim:      #9aa3b2;
    --text-faint:    #6b7382;

    --accent:        #4f9cf9;
    --accent-soft:   rgba(79, 156, 249, .14);
    --positive:      #35c98a;
    --warning:       #f0a742;
    --negative:      #ef5f6b;

    --radius:        10px;
    --radius-sm:     6px;
    --sp:            16px;

    --font: ui-sans-serif, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: light) {
    :root {
        --bg: #f6f7f9;
        --surface: #ffffff;
        --surface-2: #f0f2f5;
        --line: #dfe3e9;
        --text: #1b1f27;
        --text-dim: #5a6373;
        --text-faint: #8a93a3;
        --accent-soft: rgba(79, 156, 249, .10);
    }
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: var(--font);
    background: var(--bg);
    color: var(--text);
    font-size: 14px;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
}

/* --- Barra superiore ---------------------------------------------- */

.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--sp);
    padding: 12px 20px;
    background: var(--surface);
    border-bottom: 1px solid var(--line);
    flex-wrap: wrap;
}

.brand { display: flex; align-items: center; gap: 10px; }

.brand-mark {
    display: grid;
    place-items: center;
    width: 30px; height: 30px;
    border-radius: 8px;
    background: var(--accent);
    color: #fff;
    font-weight: 700;
    font-size: 12px;
    letter-spacing: .5px;
}

.brand-name { font-weight: 600; }

.brand-env {
    font-size: 11px;
    color: var(--text-faint);
    border: 1px solid var(--line);
    border-radius: 999px;
    padding: 1px 7px;
}

.topbar-controls { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }

.control { display: flex; align-items: center; gap: 7px; font-size: 13px; color: var(--text-dim); }

.control select {
    font: inherit;
    color: var(--text);
    background: var(--surface-2);
    border: 1px solid var(--line);
    border-radius: var(--radius-sm);
    padding: 6px 9px;
}

.btn-ghost {
    font: inherit;
    color: var(--text-dim);
    background: transparent;
    border: 1px solid var(--line);
    border-radius: var(--radius-sm);
    padding: 6px 12px;
    cursor: pointer;
}

.btn-ghost:hover { color: var(--text); border-color: var(--text-faint); }

.btn-ghost:is(a) { text-decoration: none; display: inline-block; }

.identity { gap: 10px; padding-left: 12px; border-left: 1px solid var(--line); }
.identity-name { color: var(--text); font-weight: 500; }

/* --- Barra di lavoro: dashboard salvate + periodo ------------------ */

.workbar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 10px var(--sp);
    padding: 8px var(--sp);
    background: var(--surface);
    border-bottom: 1px solid var(--line);
}

.workbar-group { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }

.workbar-label {
    font-size: 11px;
    font-weight: 600;
    letter-spacing: .06em;
    text-transform: uppercase;
    color: var(--text-faint);
}

.workbar select,
.workbar input[type="text"],
.workbar input[type="date"] {
    font: inherit;
    font-size: 13px;
    color: var(--text);
    background: var(--surface-2);
    border: 1px solid var(--line);
    border-radius: var(--radius-sm);
    padding: 6px 9px;
}

.workbar input[type="text"] { width: 200px; }

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

.workbar .btn-primary { margin-top: 0; padding: 6px 12px; font-size: 13px; }
.workbar .btn-ghost { font-size: 13px; }

.btn-ghost.is-danger:hover { color: var(--negative); border-color: var(--negative); }

.workbar-status { font-size: 12px; color: var(--text-dim); min-width: 20px; }
.workbar-status.is-error { color: var(--negative); }

.period-custom { display: inline-flex; align-items: center; gap: 6px; color: var(--text-faint); }

.workbar-toggle {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-size: 13px;
    color: var(--text-dim);
    cursor: pointer;
    user-select: none;
}

.workbar-toggle input { accent-color: var(--accent); }
.workbar-toggle:has(input:disabled) { opacity: .45; cursor: not-allowed; }

/* --- Accesso ------------------------------------------------------- */

.login-page {
    display: grid;
    place-items: center;
    min-height: 100vh;
    padding: var(--sp);
}

.login-card {
    width: 100%;
    max-width: 360px;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 28px;
}

.login-brand { margin-bottom: 22px; }
.login-card h1 { font-size: 19px; margin: 0 0 18px; font-weight: 600; }
.login-card .error { margin-bottom: 16px; }

.login-form { display: flex; flex-direction: column; gap: 14px; }

.field-block { display: flex; flex-direction: column; gap: 5px; }
.field-block span { font-size: 12px; color: var(--text-dim); }

.field-block input {
    font: inherit;
    color: var(--text);
    background: var(--surface-2);
    border: 1px solid var(--line);
    border-radius: var(--radius-sm);
    padding: 9px 11px;
}

.field-block input:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-soft);
}

.btn-primary {
    font: inherit;
    font-weight: 500;
    color: #fff;
    background: var(--accent);
    border: 0;
    border-radius: var(--radius-sm);
    padding: 10px 14px;
    cursor: pointer;
    margin-top: 4px;
}

.btn-primary:hover { filter: brightness(1.08); }

/* --- Sicurezza dell'account --------------------------------------- */

/* Le pagine del secondo fattore stanno sullo stesso riquadro del login, ma
   devono contenere un QR e un elenco di chiavi: 360px non bastano. */
.login-card-wide { max-width: 440px; }

.login-card .muted { font-size: 12px; line-height: 1.6; color: var(--text-dim); }
.login-card .notice { font-size: 12px; line-height: 1.6; }

/* Il QR arriva come SVG in linea, senza dimensioni proprie di colore: il
   fondo bianco e' necessario perche' resti leggibile anche sul tema scuro. */
.totp-qr {
    display: flex;
    justify-content: center;
    background: #fff;
    border-radius: var(--radius-sm);
    padding: 14px;
    margin: 0 0 16px;
}

.totp-qr svg { display: block; width: 220px; height: auto; }

.totp-seed {
    display: inline-block;
    margin-top: 4px;
    font-size: 14px;
    letter-spacing: .08em;
    color: var(--text);
    user-select: all;
}

.recovery-codes {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px 14px;
    list-style: none;
    margin: 0 0 16px;
    padding: 14px;
    background: var(--surface-2);
    border-radius: var(--radius-sm);
}

.recovery-codes code { font-size: 14px; letter-spacing: .06em; user-select: all; }

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

.layout {
    display: grid;
    grid-template-columns: 280px minmax(0, 1fr);
    gap: var(--sp);
    padding: var(--sp);
    align-items: start;
}

@media (max-width: 900px) {
    .layout { grid-template-columns: 1fr; }
}

/* --- Pannello campi ----------------------------------------------- */

.fields {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 14px;
    position: sticky;
    top: var(--sp);
    max-height: calc(100vh - 2 * var(--sp));
    overflow-y: auto;
}

.fields-search input {
    width: 100%;
    font: inherit;
    color: var(--text);
    background: var(--surface-2);
    border: 1px solid var(--line);
    border-radius: var(--radius-sm);
    padding: 7px 10px;
    margin-bottom: 14px;
}

.field-group h2 {
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: .08em;
    color: var(--text-faint);
    margin: 12px 0 8px;
    font-weight: 600;
}

.field-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }

.field {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 7px 9px;
    border-radius: var(--radius-sm);
    background: var(--surface-2);
    border: 1px solid transparent;
    cursor: grab;
    user-select: none;
}

.field:hover { border-color: var(--accent); background: var(--accent-soft); }
.field:active { cursor: grabbing; }
.field.is-used { opacity: .4; pointer-events: none; }
.field[hidden] { display: none; }

.field-icon {
    display: grid; place-items: center;
    width: 18px; height: 18px;
    border-radius: 4px;
    font-size: 10px;
    font-weight: 700;
    flex-shrink: 0;
    background: var(--line);
    color: var(--text-dim);
}

.field-icon.is-measure { background: rgba(53, 201, 138, .18); color: var(--positive); }
.field-icon.is-dimension { background: var(--accent-soft); color: var(--accent); }

.field-label { flex: 1; min-width: 0; }
.field-label b { display: block; font-weight: 500; font-size: 13px; }
.field-label small { display: block; color: var(--text-faint); font-size: 11px; }

.fields-hint {
    margin-top: 18px;
    padding-top: 14px;
    border-top: 1px solid var(--line);
    font-size: 12px;
    color: var(--text-faint);
    line-height: 1.6;
}

/* --- Canvas -------------------------------------------------------- */

.canvas { display: flex; flex-direction: column; gap: var(--sp); min-width: 0; }

.canvas-head h1 { font-size: 20px; margin: 0 0 4px; font-weight: 600; }
.muted { color: var(--text-dim); margin: 0; font-size: 13px; }

.dropzone {
    background: var(--surface);
    border: 1.5px dashed var(--line);
    border-radius: var(--radius);
    padding: var(--sp);
    min-height: 96px;
    transition: border-color .15s, background .15s;
}

.dropzone.is-over { border-color: var(--accent); background: var(--accent-soft); }

.dropzone-empty {
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    gap: 6px; color: var(--text-faint); min-height: 64px; text-align: center;
}

.dropzone-icon { font-size: 20px; opacity: .6; }
.dropzone-empty p { margin: 0; font-size: 13px; }

.selection { display: flex; flex-direction: column; gap: 10px; }

.chips { display: flex; flex-wrap: wrap; align-items: center; gap: 7px; min-height: 30px; }

.chips-label {
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: .06em;
    color: var(--text-faint);
    min-width: 78px;
}

.chip {
    display: inline-flex; align-items: center; gap: 6px;
    padding: 4px 8px 4px 10px;
    border-radius: 999px;
    font-size: 12px;
    background: var(--surface-2);
    border: 1px solid var(--line);
}

.chip.is-measure { border-color: rgba(53, 201, 138, .4); }
.chip.is-dimension { border-color: rgba(79, 156, 249, .4); }

.chip select {
    font: inherit; font-size: 11px;
    color: var(--text-dim);
    background: transparent;
    border: 0;
    border-left: 1px solid var(--line);
    padding-left: 6px;
    cursor: pointer;
}

.chip button {
    border: 0; background: transparent; cursor: pointer;
    color: var(--text-faint); font-size: 14px; line-height: 1;
    padding: 0 2px;
}

.chip button:hover { color: var(--negative); }

/* --- Visualizzazione ----------------------------------------------- */

.viz {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: var(--sp);
}

.viz-head {
    display: flex; align-items: center; justify-content: space-between;
    gap: 12px; margin-bottom: 14px; flex-wrap: wrap;
}

.viz-head h2 { font-size: 15px; margin: 0; font-weight: 600; }

.viz-types { display: flex; gap: 4px; }

.viz-type {
    font: inherit; font-size: 12px;
    padding: 4px 10px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--line);
    background: transparent;
    color: var(--text-dim);
    cursor: pointer;
}

.viz-type[aria-pressed="true"] {
    background: var(--accent-soft);
    border-color: var(--accent);
    color: var(--accent);
}

.viz-body { overflow-x: auto; }

/* KPI */
.kpis { display: flex; flex-wrap: wrap; gap: 12px; }

.kpi {
    flex: 1 1 170px;
    background: var(--surface-2);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 14px 16px;
}

.kpi-label {
    font-size: 12px; color: var(--text-dim);
    display: flex; align-items: center; gap: 5px;
}

.kpi-value { font-size: 25px; font-weight: 650; margin-top: 5px; letter-spacing: -.02em; }
.kpi-desc { font-size: 11px; color: var(--text-faint); margin-top: 6px; line-height: 1.45; }

/* Confronto col periodo precedente: la variazione sotto il valore. */
.kpi-delta {
    display: flex;
    align-items: baseline;
    gap: 8px;
    margin-top: 7px;
    font-size: 12px;
}

.kpi-delta small { color: var(--text-faint); font-size: 11px; }

.delta {
    font-weight: 600;
    padding: 1px 7px;
    border-radius: 999px;
}

.delta.is-up   { color: var(--positive); background: rgba(53, 201, 138, .12); }
.delta.is-down { color: var(--negative); background: rgba(239, 95, 107, .12); }
.delta.is-flat { color: var(--text-dim); background: var(--surface-2); }

.info {
    display: grid; place-items: center;
    width: 14px; height: 14px; border-radius: 50%;
    border: 1px solid var(--text-faint);
    color: var(--text-faint);
    font-size: 9px; cursor: help; flex-shrink: 0;
}

/* Barre */
.bars { display: flex; flex-direction: column; gap: 7px; }

.bar-row { display: grid; grid-template-columns: minmax(90px, 190px) 1fr auto; gap: 10px; align-items: center; }
.bar-name { font-size: 12px; color: var(--text-dim); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-track { background: var(--surface-2); border-radius: 4px; height: 20px; overflow: hidden; }
.bar-fill { height: 100%; background: linear-gradient(90deg, var(--accent), #6fb1ff); border-radius: 4px; min-width: 2px; }
.bar-value { font-size: 12px; font-variant-numeric: tabular-nums; color: var(--text); white-space: nowrap; }

/* Linee */
.line-chart { width: 100%; height: 250px; display: block; }
.line-path { fill: none; stroke: var(--accent); stroke-width: 2; stroke-linejoin: round; }
.line-area { fill: url(#lineFill); }
.line-axis { stroke: var(--line); stroke-width: 1; }
.line-label { fill: var(--text-faint); font-size: 10px; }
.line-dot { fill: var(--accent); }

/* Tabella */
.grid { width: 100%; border-collapse: collapse; font-size: 13px; }
.grid th, .grid td { padding: 8px 10px; text-align: left; border-bottom: 1px solid var(--line); }
.grid th { font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: var(--text-faint); font-weight: 600; }
.grid td.num { text-align: right; font-variant-numeric: tabular-nums; }
.grid tbody tr:hover { background: var(--surface-2); }

/* SQL */
.viz-sql { margin-top: 14px; border-top: 1px solid var(--line); padding-top: 10px; }
.viz-sql summary { font-size: 12px; color: var(--text-faint); cursor: pointer; }
.viz-sql pre {
    margin: 10px 0 0; padding: 12px;
    background: var(--surface-2); border-radius: var(--radius-sm);
    font-family: var(--mono); font-size: 11px; line-height: 1.6;
    color: var(--text-dim); overflow-x: auto; white-space: pre-wrap; word-break: break-word;
}

.error {
    background: rgba(239, 95, 107, .1);
    border: 1px solid rgba(239, 95, 107, .35);
    color: var(--negative);
    border-radius: var(--radius);
    padding: 12px 14px;
    font-size: 13px;
}

.loading { color: var(--text-faint); font-size: 13px; padding: 20px 0; text-align: center; }
