:root {
  --accent: #c0392b;
  --accent-dark: #a93226;
  --accent-soft: #fbeae8;
  --bg: #f4f5f7;
  --sidebar: #1f2430;
  --sidebar-soft: #2a3140;
  --sidebar-text: #c7cdd9;
  --card: #fff;
  --border: #e4e6eb;
  --text: #1f2430;
  --muted: #5f6573;            /* WCAG AA: 5.3:1 su --bg, 5.1:1 sui chip */
  --ok: #1f7a3d;
  --ok-soft: #e7f4ea;
  --warn: #8a5a09;
  --warn-soft: #fff4e0;
  --feat: #2a52be;
  --feat-soft: #e8eefc;
  --pos: #15803d;              /* variazione KPI positiva */
  --pos-soft: #dcfce7;
  --neg: #b91c1c;              /* variazione KPI negativa */
  --neg-soft: #fee2e2;
  --neutral-soft: #eef0f4;     /* chip/tag neutri */
  --chart-search: #2563a8;     /* serie "Ricerche" — blu acciaio, non l'indaco AI */
  --chart-click: #b8690c;      /* serie "Click" — ambra */
  --radius: 10px;
  --shadow: 0 1px 3px rgba(20,25,40,.07), 0 1px 2px rgba(20,25,40,.04);
}

* { box-sizing: border-box; }
/* [hidden] deve sempre prevalere su classi con `display`. */
[hidden] { display: none !important; }
body { margin: 0; font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; color: var(--text); background: var(--bg); -webkit-font-smoothing: antialiased; }
h1 { font-size: 22px; margin: 0; }
h2, h3 { font-size: 15px; margin: 0 0 2px; }
.muted { color: var(--muted); font-size: 13px; margin: 4px 0 0; line-height: 1.5; }
.err { color: var(--accent); font-size: 13px; }
code, .mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; }
/* Logo amastone: SVG monocromatico usato come maschera, così si ricolora via currentColor
   (bianco sulla sidebar scura, rosso brand sul login chiaro). Rapporto 340.47:62.534 ≈ 5.44:1. */
.brand-logo { display: block; aspect-ratio: 340.47 / 62.534; background-color: currentColor;
  -webkit-mask: url(logo.svg) no-repeat center / contain; mask: url(logo.svg) no-repeat center / contain; }

input, select { font: inherit; padding: 8px 10px; border: 1px solid var(--border); border-radius: 7px; background: #fff; color: var(--text); }
input:focus, select:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
input[type=checkbox] { width: auto; padding: 0; }

/* ── Buttons ─────────────────────────────────────────────── */
.btn { font: inherit; font-size: 14px; cursor: pointer; padding: 8px 14px; border: 1px solid var(--border); border-radius: 7px; background: #fff; color: var(--text); transition: background .12s, border-color .12s; white-space: nowrap; }
.btn:hover { background: #f2f3f5; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn-primary:hover { background: var(--accent-dark); }
.btn-ghost { background: transparent; border-color: rgba(255,255,255,.18); color: var(--sidebar-text); }
.btn-ghost:hover { background: rgba(255,255,255,.08); }
.btn-block { width: 100%; }
.btn-icon { padding: 6px 9px; line-height: 1; color: var(--muted); }
.btn-icon:hover { color: var(--accent); border-color: var(--accent); }

/* ── Login ───────────────────────────────────────────────── */
.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 20px; }
.login-card { background: var(--card); padding: 32px; border-radius: 14px; box-shadow: 0 10px 40px rgba(20,25,40,.12); width: 340px; display: flex; flex-direction: column; gap: 12px; }
.login-brand { color: #ed1c24; text-align: center; }
.login-brand .brand-logo { width: 210px; margin: 2px auto 0; }
.login-card > .muted { text-align: center; }
.login-card label { display: flex; flex-direction: column; gap: 5px; font-size: 13px; font-weight: 600; }
.login-card .btn { margin-top: 6px; }

/* ── App shell ───────────────────────────────────────────── */
.app-shell { display: grid; grid-template-columns: 232px 1fr; min-height: 100vh; }

.sidebar { background: var(--sidebar); color: var(--sidebar-text); display: flex; flex-direction: column; padding: 18px 14px; position: sticky; top: 0; height: 100vh; }
.brand { color: #fff; padding: 4px 4px 18px; }
.brand .brand-logo { width: 100%; }
.nav { display: flex; flex-direction: column; gap: 2px; flex: 1; }
.nav-item { display: flex; align-items: center; gap: 11px; background: none; border: none; color: var(--sidebar-text); font: inherit; font-size: 14px; text-align: left; padding: 10px 12px; border-radius: 8px; cursor: pointer; transition: background .12s, color .12s; }
.nav-item svg { width: 18px; height: 18px; fill: currentColor; opacity: .8; flex-shrink: 0; }
.nav-item:hover { background: var(--sidebar-soft); color: #fff; }
.nav-item.active { background: var(--accent); color: #fff; }
.nav-item.active svg { opacity: 1; }
.sidebar-foot { display: flex; flex-direction: column; gap: 10px; border-top: 1px solid rgba(255,255,255,.08); padding-top: 14px; }
.site-sel { display: flex; flex-direction: column; gap: 5px; font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; color: var(--sidebar-text); }
.site-sel select { background: var(--sidebar-soft); border-color: rgba(255,255,255,.14); color: #fff; }

.content { padding: 24px 28px 40px; max-width: 1600px; width: 100%; margin: 0 auto; }
.page-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; margin-bottom: 20px; flex-wrap: wrap; }
.page-actions { display: flex; gap: 8px; }

/* ── Cards ───────────────────────────────────────────────── */
.card { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px 20px; margin-bottom: 18px; }
.card-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; flex-wrap: wrap; margin-bottom: 8px; }
.card-actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.filter { min-width: 150px; }

/* Overview stat cards */
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 14px; margin-bottom: 18px; }
.stat { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px 18px; }
.stat .label { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; }
.stat .value { font-size: 26px; font-weight: 700; margin-top: 6px; }
.stat .value.sm { font-size: 17px; }
.stat .sub { font-size: 12px; color: var(--muted); margin-top: 4px; }
.quick h3 { margin-bottom: 10px; }
.quick-actions { display: flex; gap: 10px; flex-wrap: wrap; }

/* Popolarità */
#pop-card { margin-top: 18px; }
.pop-weights { display: flex; gap: 14px; align-items: flex-end; flex-wrap: wrap; margin: 12px 0 6px; }
.pop-weights label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; font-weight: 600; color: var(--muted); }
.pop-weights .filter { min-width: 90px; width: 90px; }
#pop-weights-hint { font-size: 12px; margin-bottom: 12px; }
.pop-toggle { display: flex; align-items: center; gap: 8px; font-size: 13px; cursor: pointer; }
.pop-toggle input { width: auto; }

/* Legend */
.legend .pill, .pill { display: inline-block; background: var(--accent-soft); color: var(--accent-dark); border-radius: 20px; padding: 2px 10px; font-size: 12px; font-weight: 600; margin: 0 2px; }

/* ── Grid / tables ───────────────────────────────────────── */
/* Le tabelle larghe scorrono in orizzontale entro la card invece di
   sfondare il viewport su schermi stretti. */
.table-scroll { width: 100%; overflow-x: auto; }
.grid { width: 100%; border-collapse: collapse; margin-top: 10px; }
.grid th, .grid td { text-align: left; padding: 10px 10px; border-bottom: 1px solid var(--border); font-size: 14px; vertical-align: middle; }
.grid th { font-size: 11px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; border-bottom-width: 2px; }
.grid tr:last-child td { border-bottom: none; }
.grid input, .grid select { width: 100%; }
.grid .col-act { width: 44px; text-align: center; }
.grid .col-on { width: 64px; text-align: center; }
.grid .col-on input { width: auto; }
.grid .col-prio { width: 110px; }
.boost-grid td.col-prio input { width: 80px; }
.row-off td:not(.col-on):not(.col-act) { opacity: .45; }
.val-custom { display: flex; gap: 6px; align-items: center; }
.val-custom input { flex: 1; }
.grid .col-cnt { width: 92px; white-space: nowrap; color: var(--muted); }

/* Conteggio prodotti per regola */
.cnt-badge { display: inline-block; background: var(--neutral-soft); color: var(--text); border-radius: 6px; padding: 2px 8px; font-size: 12px; font-weight: 600; }
.cnt-badge.zero { color: var(--accent); background: var(--accent-soft); }
.cnt-badge.pin { background: var(--accent); color: #fff; }

/* Product picker (campo Prodotto) */
.picker { position: relative; }
.picker > input { width: 100%; }
.picker-menu { position: absolute; top: calc(100% + 2px); left: 0; right: 0; z-index: 50; background: #fff; border: 1px solid var(--border); border-radius: 8px; box-shadow: 0 8px 24px rgba(20,25,40,.16); max-height: 260px; overflow: auto; }
.picker-opt { padding: 8px 10px; cursor: pointer; font-size: 13px; display: flex; justify-content: space-between; gap: 10px; align-items: baseline; }
.picker-opt:hover { background: var(--accent-soft); }
.picker-opt .picker-id { color: var(--muted); font-family: ui-monospace, monospace; font-size: 11px; flex-shrink: 0; }
.picker-empty { padding: 10px; font-size: 12px; color: var(--muted); }

/* Prodotto con thumbnail (playground) */
.prod-cell { display: flex; align-items: center; gap: 10px; }
.thumb { width: 42px; height: 42px; border-radius: 6px; background: #f0f0f0; flex-shrink: 0; overflow: hidden; display: flex; align-items: center; justify-content: center; }
.thumb img { width: 100%; height: 100%; object-fit: cover; }

/* Stato prodotto (playground) */
.tags-cell { display: flex; gap: 5px; flex-wrap: wrap; }
.tag { display: inline-block; padding: 2px 8px; border-radius: 20px; font-size: 11px; font-weight: 600; background: var(--neutral-soft); color: var(--muted); }
.tag.in { background: var(--ok-soft); color: var(--ok); }
.tag.out { background: var(--accent-soft); color: var(--accent); }
.tag.sale { background: var(--warn-soft); color: var(--warn); }
.tag.feat { background: var(--feat-soft); color: var(--feat); }

.empty { text-align: center; color: var(--muted); padding: 18px; font-size: 14px; }

/* Badges */
.badge { display: inline-block; padding: 3px 10px; border-radius: 20px; font-size: 12px; font-weight: 600; }
.badge.completed { background: var(--ok-soft); color: var(--ok); }
.badge.running { background: var(--warn-soft); color: var(--warn); }
.badge.failed { background: var(--accent-soft); color: var(--accent); }
.prio-badge { display: inline-block; min-width: 30px; text-align: center; background: var(--accent); color: #fff; border-radius: 6px; padding: 3px 10px; font-weight: 700; }

/* ── Simulator ───────────────────────────────────────────── */
.sim-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; margin: 8px 0 14px; }
.sim-grid label { display: flex; flex-direction: column; gap: 5px; font-size: 12px; font-weight: 600; color: var(--muted); }
.sim-grid label.chk { flex-direction: row; align-items: center; gap: 7px; font-size: 13px; color: var(--text); }
.sim-result { font-size: 15px; font-weight: 600; display: flex; align-items: center; gap: 10px; flex-wrap: wrap; border-top: 1px solid var(--border); padding-top: 14px; }
.sim-result .muted { font-weight: 400; }
.chk { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; }
.chk.auto { color: var(--muted); }
.rng { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }
.rng input { width: auto; }

/* ── Statistiche ─────────────────────────────────────────── */
.stats-bar { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.ov-section-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin: 2px 0 12px; }
.ov-section-head h3 { margin: 0; font-size: 15px; }
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 14px; margin-bottom: 16px; }
.kpi { background: var(--surface, #fff); border: 1px solid var(--border); border-radius: 12px; padding: 16px 18px; }
.kpi-val { font-size: 26px; font-weight: 700; line-height: 1.1; }
.kpi-label { font-size: 13px; color: var(--text); margin-top: 4px; font-weight: 600; }
.kpi-sub { font-size: 12px; margin-top: 2px; }
.kpi-var { display: inline-flex; align-items: center; gap: 2px; font-size: 12px; font-weight: 600; margin-top: 10px; padding: 2px 8px; border-radius: 999px; }
.kpi-var.good { color: var(--pos); background: var(--pos-soft); }
.kpi-var.bad { color: var(--neg); background: var(--neg-soft); }
.kpi-var.flat { color: var(--muted); background: rgba(0,0,0,.05); }
.kpi-arrow { font-size: 9px; }
.kpi-vs { font-weight: 400; margin-left: 4px; }

.chart-wrap { width: 100%; overflow-x: auto; }
.chart-svg { width: 100%; height: auto; display: block; }
.chart-grid { stroke: var(--border); stroke-width: 1; }
.chart-axis { fill: var(--muted); font-size: 10px; }
.chart-line-s { stroke: var(--chart-search); stroke-width: 2; }
.chart-line-c { stroke: var(--chart-click); stroke-width: 2; }
.chart-legend { display: inline-flex; gap: 14px; font-size: 12px; }
.lg { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); }
.lg::before { content: ''; width: 12px; height: 3px; border-radius: 2px; display: inline-block; }
.lg-s::before { background: var(--chart-search); }
.lg-c::before { background: var(--chart-click); }

/* ── Toasts ──────────────────────────────────────────────── */
.toasts { position: fixed; bottom: 20px; right: 20px; display: flex; flex-direction: column; gap: 10px; z-index: 1000; }
.toast { display: flex; align-items: flex-start; gap: 10px; background: #fff; border: 1px solid var(--border); border-radius: 8px; padding: 12px 16px; box-shadow: 0 6px 24px rgba(20,25,40,.16); font-size: 14px; font-weight: 500; max-width: 360px; animation: toast-in .2s ease; }
.toast.ok { border-color: var(--ok); }
.toast.ko { border-color: var(--accent); }
.toast-icon { flex-shrink: 0; width: 18px; height: 18px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 11px; font-weight: 700; color: #fff; margin-top: 1px; }
.toast.ok .toast-icon { background: var(--ok); }
.toast.ko .toast-icon { background: var(--accent); }
.toast-msg { line-height: 1.4; }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

/* ── Panoramica: catalogo + ultime sync ──────────────────── */
.ov-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; align-items: start; margin-bottom: 18px; }
.ov-grid .card { margin-bottom: 0; }
.bar-row { display: grid; grid-template-columns: 92px 1fr 92px; align-items: center; gap: 12px; margin: 11px 0; }
.bar-row .bar-label { font-size: 13px; }
.bar-track { background: var(--bg); border-radius: 6px; height: 9px; overflow: hidden; }
.bar-fill { height: 100%; width: 100%; border-radius: 6px; background: var(--accent); transform: scaleX(0); transform-origin: left center; transition: transform .3s ease; }
.bar-fill.ok { background: var(--ok); }
.bar-fill.ko { background: var(--accent); }
.bar-fill.warn { background: var(--warn); }
.bar-fill.feat { background: #2a52be; }
.bar-row .bar-val { font-size: 12px; font-weight: 600; text-align: right; color: var(--muted); white-space: nowrap; }
.chips { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 14px; }
.chip { background: var(--bg); border: 1px solid var(--border); border-radius: 20px; padding: 4px 12px; font-size: 13px; color: var(--muted); }
.chip b { color: var(--text); font-weight: 700; }
.mini-list { display: flex; flex-direction: column; }
.mini-row { display: flex; align-items: center; gap: 10px; padding: 9px 0; border-bottom: 1px solid var(--border); font-size: 13px; }
.mini-row:last-child { border-bottom: none; }
.mini-row .mini-when { margin-left: auto; color: var(--muted); font-size: 12px; white-space: nowrap; }

/* ── Righe cliccabili (playground) ───────────────────────── */
.grid tr.row-click { cursor: pointer; }
.grid tr.row-click:hover td { background: var(--accent-soft); }

/* ── Modale dettaglio prodotto ───────────────────────────── */
/* <dialog> in top-layer: il dialog stesso fa da overlay a tutto schermo, così
   il click sul padding chiude e la gestione focus/Esc è nativa. */
.modal { inset: 0; width: 100%; max-width: 100%; height: 100%; max-height: 100%; margin: 0; border: none; padding: 40px 16px; background: rgba(20,25,40,.45); overflow-y: auto; }
.modal[open] { display: flex; align-items: flex-start; justify-content: center; animation: toast-in .15s ease; }
.modal::backdrop { background: transparent; }
.modal-box { background: var(--card); border-radius: var(--radius); box-shadow: 0 20px 60px rgba(20,25,40,.3); width: 100%; max-width: 680px; height: max-content; }
.modal-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 16px 20px; border-bottom: 1px solid var(--border); }
.modal-title-wrap { display: flex; align-items: center; gap: 12px; min-width: 0; }
.modal-title-wrap h2 { font-size: 16px; line-height: 1.3; word-break: break-word; }
.modal-close { font-size: 26px; line-height: 1; background: none; border: none; color: var(--muted); cursor: pointer; padding: 0 4px; }
.modal-close:hover { color: var(--accent); }
.modal-body { padding: 16px 20px 20px; }
.detail-h { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 18px 0 6px; }
.detail-grid { width: 100%; border-collapse: collapse; }
.detail-grid th, .detail-grid td { text-align: left; padding: 6px 10px; border-bottom: 1px solid var(--border); font-size: 13px; vertical-align: top; }
.detail-grid th { width: 38%; color: var(--muted); font-weight: 600; white-space: nowrap; }
.detail-grid td { word-break: break-word; }
.detail-json { background: #1f2430; color: #e6e9ef; border-radius: 8px; padding: 14px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; line-height: 1.5; overflow-x: auto; max-height: 360px; margin: 0 0 12px; white-space: pre; }

/* ── Responsive ──────────────────────────────────────────── */
@media (max-width: 820px) {
  .app-shell { grid-template-columns: 1fr; }
  .sidebar { position: static; height: auto; flex-direction: row; flex-wrap: wrap; align-items: center; gap: 8px; }
  .brand { padding: 4px 8px; }
  .nav { flex-direction: row; flex: 1 1 100%; order: 3; overflow-x: auto; }
  .nav-item { white-space: nowrap; }
  .sidebar-foot { flex-direction: row; align-items: center; border: none; padding: 0; margin-left: auto; }
  .content { padding: 18px; }
  .ov-grid { grid-template-columns: 1fr; }
}

/* Target touch ≥44px su puntatori grossolani (tocco). */
@media (pointer: coarse) {
  .btn, .nav-item { min-height: 44px; }
  .btn-icon { min-width: 44px; min-height: 44px; }
}

/* Rispetta prefers-reduced-motion: niente animazioni/transizioni significative. */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .001ms !important;
    scroll-behavior: auto !important;
  }
}
