:root {
    --bg: #0f1115;
    --panel: #171a21;
    --panel-2: #1e222b;
    --line: #2a2f3a;
    --text: #e8eaef;
    --muted: #8b93a3;
    --accent: #e5484d;
    --accent-soft: rgba(229, 72, 77, .15);
    --good: #3fb97c;
    --good-soft: rgba(63, 185, 124, .16);
    --warn: #f0a33a;
    --info: #5b9cf5;
    --seat-free: #3a404d;
    --radius: 10px;
    --font: "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
    color-scheme: dark;
}

@media (prefers-color-scheme: light) {
    :root {
        --bg: #f4f5f8;
        --panel: #ffffff;
        --panel-2: #f0f2f6;
        --line: #e1e4ea;
        --text: #16181d;
        --muted: #667085;
        --accent-soft: rgba(229, 72, 77, .10);
        --good: #1f9d5e;
        --good-soft: rgba(31, 157, 94, .12);
        --seat-free: #d5d9e1;
        color-scheme: light;
    }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
    background: var(--bg);
    color: var(--text);
    font: 14px/1.45 var(--font);
    -webkit-font-smoothing: antialiased;
}
button, input, select { font: inherit; color: inherit; }
h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 0 0 12px; font-weight: 600; }
h3 { font-size: 14px; margin: 22px 0 10px; }
.muted { color: var(--muted); }
.small { font-size: 12px; font-weight: 400; }
[hidden] { display: none !important; }

/* ---------- topbar ---------- */
.topbar {
    position: sticky; top: 0; z-index: 10;
    display: flex; align-items: center; gap: 20px;
    padding: 12px 24px;
    background: color-mix(in srgb, var(--bg) 88%, transparent);
    backdrop-filter: blur(8px);
    border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 17px; }
.live { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); }
.live .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.live.on .dot { background: var(--good); box-shadow: 0 0 0 0 var(--good); animation: pulse 2s infinite; }
.live.off .dot { background: var(--accent); }
@keyframes pulse { 0% { box-shadow: 0 0 0 0 rgba(63,185,124,.6); } 70% { box-shadow: 0 0 0 8px rgba(63,185,124,0); } 100% { box-shadow: 0 0 0 0 rgba(63,185,124,0); } }
.backlog { font-size: 12px; color: var(--warn); background: rgba(240, 163, 58, .12); padding: 2px 8px; border-radius: 999px; }
.rate { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.follow-all { margin-top: 8px; }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 16px; }
.clock { font-variant-numeric: tabular-nums; color: var(--muted); }

.btn {
    border: 1px solid var(--line); background: var(--panel-2);
    padding: 7px 14px; border-radius: 8px; cursor: pointer; white-space: nowrap;
}
.btn:hover { border-color: var(--muted); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn-primary:hover { filter: brightness(1.08); border-color: var(--accent); }
.btn:disabled { opacity: .6; cursor: wait; }
.icon-btn { background: none; border: 0; cursor: pointer; color: var(--muted); padding: 4px 6px; border-radius: 6px; font-size: 14px; }
.icon-btn:hover { color: var(--text); background: var(--panel-2); }

/* ---------- page ---------- */
.page { padding: 20px 24px 40px; max-width: 1700px; margin: 0 auto; }

.kpis { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 12px; margin-bottom: 16px; }
.kpi { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.kpi-label { font-size: 12px; color: var(--muted); }
/* Il corpo si riduce al crescere della cifra (classi messe da setKpi), così il
   numero resta su una riga anche con "1.269.907 €". */
/* Il corpo lo riduce fitKpi() finché la cifra non entra nel riquadro. */
.kpi-value { font-size: 26px; font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }
/* Il sottotitolo può andare a capo: meglio due righe che testo tagliato o fuori dal riquadro. */
.kpi-sub { font-size: 12px; color: var(--muted); overflow-wrap: anywhere; }
.kpi-sub.small { font-size: 11px; }
.kpi-value.bump { animation: bump .6s; }
@keyframes bump { 30% { color: var(--good); transform: scale(1.04); } }

.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; margin-bottom: 14px; }
.toolbar select, .toolbar input[type=search], .row-form input, .row-form select {
    background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 7px 10px; min-width: 0;
}
.toolbar input[type=search] { width: 220px; }
/* Con settimane di programmazione i giorni scorrono in orizzontale su una riga. */
.date-chips {
    display: flex; gap: 6px; flex: 1 1 100%; overflow-x: auto; padding-bottom: 4px;
    scrollbar-width: thin; scroll-snap-type: x proximity;
}
/* Fila delle date: "Tutto il periodo" e "Periodo…" restano fermi, i giorni scorrono. */
.dates-row { display: flex; gap: 6px; flex: 1 1 100%; min-width: 0; position: relative; align-items: flex-start; }
.dates-row .date-chips { flex: 1 1 auto; min-width: 0; }
.period-chip { font-weight: 600; }
.chip.period-chip.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.chip.inrange { background: var(--accent-soft); color: var(--accent); border-color: var(--accent); }
/* Scheda film: Cinema / Province / Spettacoli. */
.film-tabs-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin: 18px 0 8px; }
.film-tabs-head .segmented { margin-left: 0; }
.film-tabs-head #fFilter { margin-left: auto; }
/* Mini calendario del periodo. */
.cal-pop {
    position: absolute; top: calc(100% + 6px); left: 0; z-index: 30; background: var(--panel);
    border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; box-shadow: 0 10px 30px rgba(0, 0, 0, .35);
    display: flex; flex-direction: column; gap: 10px; max-width: calc(100vw - 32px);
}
.cal-pop[hidden] { display: none; }
.cal-months { display: flex; gap: 16px; flex-wrap: wrap; }
.cal-month h4 { margin: 0 0 6px; font-size: 13px; text-transform: capitalize; }
.cal-grid { display: grid; grid-template-columns: repeat(7, 32px); gap: 2px; }
.cal-grid .dow { font-size: 11px; color: var(--muted); text-align: center; padding-bottom: 2px; }
.cal-grid button {
    height: 30px; border: 0; border-radius: 6px; background: none; cursor: pointer; font-size: 13px; color: var(--text);
    font-variant-numeric: tabular-nums;
}
.cal-grid button:hover:not(:disabled) { background: var(--panel-2); }
.cal-grid button:disabled { color: var(--muted); opacity: .35; cursor: default; }
.cal-grid button.today { box-shadow: inset 0 0 0 1px var(--line); }
.cal-grid button.inrange { background: var(--accent-soft); color: var(--accent); border-radius: 0; }
.cal-grid button.edge { background: var(--accent); color: #fff; border-radius: 6px; }
.cal-foot { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; font-size: 13px; }
.cal-foot .muted { flex: 1 1 auto; }
.chip { border: 1px solid var(--line); background: var(--panel); border-radius: 999px; padding: 5px 12px; cursor: pointer; font-size: 13px; white-space: nowrap; flex: none; scroll-snap-align: start; }
.chip.past { opacity: .6; }
.chip.active { background: var(--text); color: var(--bg); border-color: var(--text); }
.chip .n { color: var(--muted); margin-left: 4px; font-size: 12px; }
.chip.active .n { color: inherit; opacity: .7; }
.check { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 13px; cursor: pointer; }
.segmented { margin-left: auto; display: flex; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 2px; }
.segmented button { border: 0; background: none; padding: 5px 12px; border-radius: 6px; cursor: pointer; color: var(--muted); }
.segmented button.active { background: var(--panel-2); color: var(--text); font-weight: 600; }
/* Circuito: sta a sinistra e, quando è attivo, si vede che i dati sono filtrati. */
.circuits { margin-left: 0; }
.circuits button.active.filtered { background: var(--accent-soft); color: var(--accent); }

.more { display: flex; align-items: center; justify-content: center; gap: 12px; padding: 12px; border-top: 1px solid var(--line); color: var(--muted); font-size: 13px; }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: var(--panel-2); }
.grid.compact td, .grid.compact th { padding-top: 6px; padding-bottom: 6px; }

/* Scheda film: venduti per giorno. */
.days { display: flex; gap: 4px; overflow-x: auto; padding-bottom: 6px; }
.days .day {
    flex: 1 0 44px; display: flex; flex-direction: column; align-items: center; gap: 4px;
    background: none; border: 1px solid transparent; border-radius: 8px; padding: 6px 2px; cursor: pointer; color: var(--text);
}
.days .day:hover { border-color: var(--line); }
.days .day.active { border-color: var(--text); }
.days .day.past { opacity: .55; }
.days .v { font-size: 11px; font-variant-numeric: tabular-nums; }
.days .bar { height: 90px; width: 18px; display: flex; align-items: flex-end; background: var(--panel-2); border-radius: 4px; overflow: hidden; }
.days .bar i { display: block; width: 100%; background: var(--accent); border-radius: 4px 4px 0 0; min-height: 1px; }
.days .l { font-size: 11px; color: var(--muted); white-space: nowrap; }

.layout { display: grid; grid-template-columns: minmax(0, 1fr) 340px; gap: 16px; align-items: start; }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }
.table-panel { padding: 0; overflow: hidden; }
.side { display: flex; flex-direction: column; gap: 16px; position: sticky; top: 74px; }

/* ---------- table ---------- */
.table-wrap { overflow-x: auto; }
.grid { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.grid th {
    text-align: left; font-size: 12px; font-weight: 600; color: var(--muted);
    padding: 10px 12px; border-bottom: 1px solid var(--line); white-space: nowrap;
    cursor: pointer; user-select: none; background: var(--panel);
}
.grid th.num, .grid td.num { text-align: right; }
.grid th.sorted::after { content: " ↓"; }
.grid th.sorted.asc::after { content: " ↑"; }
.grid td { padding: 9px 12px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.grid tbody tr:last-child td { border-bottom: 0; }
.grid tbody tr:hover { background: var(--panel-2); }
/* Icone sempre visibili anche quando la tabella scorre in orizzontale. */
.grid .sticky-actions { position: sticky; right: 0; background: var(--panel); box-shadow: -8px 0 8px -8px rgba(0, 0, 0, .35); }
.grid tbody tr:hover .sticky-actions { background: var(--panel-2); }
/* Riepilogo del circuito: prima riga della tabella, staccata dalle altre. */
.grid tr.total-row td {
    background: var(--accent-soft); border-bottom: 2px solid var(--line); font-weight: 600;
}
.grid tr.total-row .film b { color: var(--accent); }
/* Quota di mercato del circuito, sotto ai biglietti venduti. */
.grid tr.total-row .share { display: block; font-size: 11px; font-weight: 600; color: var(--accent); cursor: help; }
.grid tr.total-row:hover td { background: var(--accent-soft); }
/* Classifica delle province: posizione, quota sotto ai venduti, provincia scelta. */
.grid .rank { display: inline-block; min-width: 26px; color: var(--muted); font-weight: 600; }
.grid td .share { display: block; font-size: 11px; font-weight: 600; color: var(--accent); cursor: help; }
.grid tr.selected td { background: var(--accent-soft); }
/* Menù provincia: come i circuiti, si vede quando i dati sono filtrati. */
.toolbar select.filtered { background: var(--accent-soft); color: var(--accent); border-color: var(--accent); }
.grid tr.ended { opacity: .55; }
.grid tr.flash { animation: flash 2.5s ease-out; }
@keyframes flash { 0% { background: var(--good-soft); } 100% { background: transparent; } }
.time { font-weight: 700; font-size: 15px; }
.film { font-weight: 600; min-width: 180px; max-width: 320px; }
.film small, .where small { display: block; font-weight: 400; color: var(--muted); font-size: 12px; }
.where { white-space: nowrap; }
.pos { color: var(--good); font-weight: 600; }
.neg { color: var(--accent); font-weight: 600; }
.zero { color: var(--muted); }
.occ { display: flex; align-items: center; gap: 8px; min-width: 130px; }
.bar { flex: 1; height: 6px; background: var(--panel-2); border-radius: 3px; overflow: hidden; display: flex; }
.bar i { display: block; height: 100%; background: var(--accent); }
.bar i.held { background: var(--warn); }
.occ span { width: 40px; text-align: right; font-size: 13px; }
.status-err { color: var(--accent); cursor: help; }
.status-pending { color: var(--muted); }
.actions { white-space: nowrap; text-align: right; }
.badge { display: inline-block; font-size: 11px; padding: 1px 6px; border-radius: 4px; background: var(--panel-2); color: var(--muted); margin-left: 6px; vertical-align: 1px; }

.empty { padding: 48px 24px; text-align: center; color: var(--muted); }

/* ---------- side ---------- */
.hourly svg { display: block; width: 100%; height: auto; }
.hourly .axis { fill: var(--muted); font-size: 10px; }
.hourly rect.b { fill: var(--accent); }
.hourly rect.b.now { fill: var(--good); }
.hourly .total { font-size: 13px; color: var(--muted); margin-top: 6px; }

.feed-panel { max-height: calc(100vh - 420px); min-height: 200px; display: flex; flex-direction: column; }
.feed { list-style: none; margin: 0; padding: 0; overflow-y: auto; }
.feed li { display: grid; grid-template-columns: 44px 1fr auto; gap: 8px; align-items: center; padding: 8px 0; border-bottom: 1px solid var(--line); font-size: 13px; }
.feed li:last-child { border-bottom: 0; }
.feed li.new { animation: flash 2.5s ease-out; }
.feed .d { font-weight: 700; font-size: 15px; text-align: center; border-radius: 6px; padding: 2px 0; }
.feed .d.pos { background: var(--good-soft); }
.feed .d.neg { background: var(--accent-soft); }
.feed .t { min-width: 0; }
.feed .t b { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.feed .t span, .feed .ago { color: var(--muted); font-size: 12px; }
.feed li.feed-empty { display: block; color: var(--muted); font-size: 13px; border: 0; }

/* ---------- modal ---------- */
.modal { position: fixed; inset: 0; z-index: 50; background: rgba(0,0,0,.55); display: flex; align-items: flex-start; justify-content: center; padding: 6vh 16px; overflow-y: auto; }
.modal-card { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; width: 100%; max-width: 620px; box-shadow: 0 20px 60px rgba(0,0,0,.4); }
.modal-card.wide { max-width: 1000px; }
.modal-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; padding: 18px 20px 0; }
.modal-head h2 { font-size: 18px; text-transform: none; letter-spacing: 0; color: var(--text); margin: 0; }
.modal-head p { margin: 4px 0 0; }
.modal-body { padding: 4px 20px 20px; }
.modal-body h3:first-child { margin-top: 14px; }
.row-form { display: flex; gap: 8px; }
.row-form input, .row-form select:first-child { flex: 1; }
.hint { color: var(--muted); font-size: 12px; margin: 6px 0 0; }
.form-msg { min-height: 20px; font-size: 13px; margin: 14px 0 0; }
.form-msg.err { color: var(--accent); }
.form-msg.ok { color: var(--good); }
.follows { list-style: none; padding: 0; margin: 10px 0 0; max-height: 240px; overflow-y: auto; }
.follows li { display: flex; align-items: center; justify-content: space-between; padding: 6px 10px; background: var(--panel-2); border-radius: 8px; margin-bottom: 6px; font-size: 13px; }

.detail-stats { display: grid; grid-template-columns: repeat(5, 1fr); gap: 10px; margin-top: 14px; }
.detail-stats div { background: var(--panel-2); border-radius: 8px; padding: 10px 12px; }
.detail-stats b { display: block; font-size: 20px; font-variant-numeric: tabular-nums; }
.detail-stats span { font-size: 12px; color: var(--muted); }
.history svg { display: block; width: 100%; height: auto; }
.history .line { fill: none; stroke: var(--accent); stroke-width: 2; }
.history .area { fill: var(--accent-soft); }
.history .cap { stroke: var(--muted); stroke-dasharray: 4 4; }
.history .axis { fill: var(--muted); font-size: 10px; }

.legend { display: flex; gap: 16px; font-size: 12px; color: var(--muted); margin-bottom: 10px; }
.legend span { display: flex; align-items: center; gap: 6px; }
.seat { display: inline-block; width: 12px; height: 12px; border-radius: 3px 3px 2px 2px; }
.s-free { background: var(--seat-free); }
.s-sold { background: var(--accent); }
.s-held { background: var(--warn); }
.s-special { background: var(--info); }
.seatmap { overflow-x: auto; padding: 8px 0 4px; }
.seatmap .screen { margin: 0 auto 18px; width: 60%; text-align: center; font-size: 11px; letter-spacing: .3em; color: var(--muted); border-top: 3px solid var(--muted); border-radius: 50% 50% 0 0 / 12px 12px 0 0; padding-top: 6px; }
.seatmap .srow { display: flex; align-items: center; gap: 3px; margin: 3px 0; justify-content: center; width: max-content; min-width: 100%; }
.seatmap .lbl { width: 18px; text-align: center; font-size: 10px; color: var(--muted); }
.seatmap .seat, .seatmap .gap { width: 13px; height: 13px; flex: none; }
.areas { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 14px; }
.areas div { font-size: 12px; background: var(--panel-2); border-radius: 8px; padding: 6px 10px; border-left: 4px solid var(--c, var(--muted)); }
.loading { color: var(--muted); padding: 20px 0; }

/* ---------- toasts ---------- */
.toasts { position: fixed; right: 20px; bottom: 20px; display: flex; flex-direction: column; gap: 8px; z-index: 60; }
.toast { background: var(--panel); border: 1px solid var(--line); border-left: 4px solid var(--good); border-radius: 8px; padding: 10px 14px; font-size: 13px; box-shadow: 0 8px 24px rgba(0,0,0,.25); animation: toastIn .25s ease-out; max-width: 340px; }
.toast.err { border-left-color: var(--accent); }
@keyframes toastIn { from { transform: translateY(8px); opacity: 0; } }

@media (max-width: 1200px) {
    .layout { grid-template-columns: 1fr; }
    .side { position: static; display: grid; grid-template-columns: 1fr 1fr; }
    .feed-panel { max-height: 400px; }
}
@media (max-width: 800px) {
    .page { padding: 16px; }
    .topbar { padding: 10px 16px; gap: 12px; }
    .clock { display: none; }
    .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .side { grid-template-columns: 1fr; }
    .segmented { margin-left: 0; }
    .toolbar input[type=search] { width: 100%; }
    .detail-stats { grid-template-columns: repeat(2, 1fr); }
    .row-form { flex-wrap: wrap; }
}
