:root {
  color-scheme: dark;
  --bg: #0b1220; --panel: #111a2c; --ink: #e2e8f0; --muted: #8fa0b5;
  --line: #243349; --accent: #3b82f6; --accent-soft: #1a2a45;
  --verde: #4ade80; --verde-bg: #113321;
  --giallo: #facc15; --giallo-bg: #3a3010;
  --rosso: #f87171; --rosso-bg: #3d1a1e;
  --sidebar: #0d1526; --sidebar-ink: #cbd5e1;
  --thumb: #3b4d68;
  --thead-bg: #182338; --thead-hover: #1f2e49;
  --detail-bg: #0e1728; --tot-bg: #182338;
  --desc-ink: #b7c3d4;
  --img-bg: #f8fafc;
  --err-line: #7f2a33;
}
* { box-sizing: border-box; }
html { scrollbar-width: thin; scrollbar-color: var(--thumb) var(--bg); }
body { margin: 0; font: 14px/1.45 "Segoe UI", system-ui, sans-serif; background: var(--bg); color: var(--ink); }
.layout { display: flex; min-height: 100vh; }

/* Scrollbar coerenti con la palette */
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--thumb); border-radius: 8px; border: 2px solid var(--bg); }
::-webkit-scrollbar-thumb:hover { background: var(--accent); }
#portal-nav { scrollbar-width: thin; scrollbar-color: #33415a transparent; }
#portal-nav::-webkit-scrollbar-thumb { background: #33415a; border: 2px solid var(--sidebar); }
#portal-nav::-webkit-scrollbar-thumb:hover { background: var(--accent); }
.table-scroll { scrollbar-color: var(--thumb) var(--panel); }
.table-scroll::-webkit-scrollbar-thumb { border-color: var(--panel); }

/* Sidebar */
.sidebar { width: 230px; flex: 0 0 230px; background: var(--sidebar); color: var(--sidebar-ink); display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh; }
.brand { padding: 20px 18px 14px; font-size: 19px; font-weight: 700; color: #fff; }
.brand span { display: block; font-size: 11px; font-weight: 400; color: #8fa0b5; margin-top: 2px; }
#portal-nav { flex: 1; overflow-y: auto; padding: 4px 10px; }
.portal { margin-bottom: 6px; }
.portal-name { padding: 9px 10px 5px; font-size: 12px; letter-spacing: .06em; text-transform: uppercase; color: #8fa0b5; display: flex; justify-content: space-between; align-items: center; }
.portal-name .soon { font-size: 10px; background: #26334a; border-radius: 8px; padding: 1px 7px; text-transform: none; letter-spacing: 0; }
.portal.disabled { opacity: .45; }
.global-portal .portal-name { color: #7dd3fc; }
.global-portal { border-bottom: 1px solid #1e293b; padding-bottom: 6px; margin-bottom: 10px; }
#info-section { padding: 4px 0; }
#info-section .card h3 { font-size: 16px; }
.nav-item { display: flex; justify-content: space-between; align-items: center; padding: 8px 10px; margin: 2px 0; border-radius: 8px; cursor: pointer; color: var(--sidebar-ink); border: 0; background: none; width: 100%; text-align: left; font: inherit; }
.nav-item:hover { background: #1c2739; color: #fff; }
.nav-item.active { background: var(--accent); color: #fff; }
.nav-item .n { font-size: 11px; background: rgba(255,255,255,.14); padding: 1px 8px; border-radius: 9px; }
.sidebar-footer { padding: 12px 16px 16px; font-size: 11px; color: #8fa0b5; border-top: 1px solid #1e293b; }
.disclaimer { margin-top: 8px; line-height: 1.4; }

/* Main */
.main { flex: 1; min-width: 0; padding: 18px 22px 40px; }
.topbar { display: flex; flex-wrap: wrap; gap: 12px; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.topbar h1 { margin: 0; font-size: 21px; }
.title-row { display: flex; align-items: center; gap: 10px; min-width: 0; }
.nav-toggle { display: none; align-items: center; justify-content: center; width: 40px; height: 40px; flex: 0 0 auto; border: 1px solid var(--line); border-radius: 10px; background: var(--panel); color: var(--ink); font-size: 18px; line-height: 1; cursor: pointer; }
.nav-toggle:hover { border-color: var(--accent); background: var(--accent-soft); }
.nav-backdrop { display: none; }
.controls { display: flex; gap: 8px; flex-wrap: wrap; }
.controls input, .controls select, .controls button { padding: 7px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel); font: inherit; color: var(--ink); }
.controls input { width: 260px; }
.controls button { cursor: pointer; white-space: nowrap; }
.controls button:hover { background: var(--accent-soft); border-color: var(--accent); }
.hidden { display: none !important; }
.count { color: var(--muted); font-size: 12px; margin-bottom: 6px; }

/* Dashboard */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; margin-bottom: 18px; }
.kpi { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 14px 16px; min-width: 0; }
.kpi .v { font-size: 26px; font-weight: 700; }
.kpi .l { color: var(--muted); font-size: 12px; margin-top: 2px; }
.kpi.clickable { cursor: pointer; }
.kpi.clickable:hover { border-color: var(--accent); }
.dash-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.dash-grid > .card { min-width: 0; }
@media (max-width: 1100px) { .dash-grid { grid-template-columns: 1fr; } }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 14px 16px; }
.card h3 { margin: 0 0 10px; font-size: 14px; }
.mini-row { display: flex; justify-content: space-between; gap: 10px; padding: 7px 0; border-top: 1px solid var(--line); cursor: pointer; min-width: 0; overflow-wrap: anywhere; }
.mini-row:hover { background: var(--accent-soft); }
.mini-row .t { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; flex: 1 1 auto; }
.mini-row .v { flex: 0 0 auto; font-weight: 600; white-space: nowrap; }

/* Table */
.table-scroll { overflow: auto; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; max-height: calc(100vh - 170px); }
table { border-collapse: collapse; width: 100%; font-size: 13px; }
thead th { position: sticky; top: 0; background: var(--thead-bg); text-align: left; padding: 9px 10px; border-bottom: 2px solid var(--line); white-space: nowrap; cursor: pointer; user-select: none; z-index: 2; }
thead th:hover { background: var(--thead-hover); }
thead th .arrow { color: var(--accent); font-size: 10px; }
tbody td { padding: 8px 10px; border-bottom: 1px solid var(--line); white-space: nowrap; max-width: 340px; overflow: hidden; text-overflow: ellipsis; vertical-align: top; }
tbody tr.row:hover { background: var(--accent-soft); cursor: pointer; }
td.num { text-align: right; font-variant-numeric: tabular-nums; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
.id-link { display: inline-block; max-width: 130px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; vertical-align: bottom; }

/* Badges */
.badge { display: inline-block; padding: 2px 9px; border-radius: 10px; font-size: 11px; font-weight: 600; }
.badge.verde, .badge.Alto, .badge.Selezionato { background: var(--verde-bg); color: var(--verde); }
.badge.giallo, .badge.Medio { background: var(--giallo-bg); color: var(--giallo); }
.badge.rosso, .badge.Basso, .badge.Escluso { background: var(--rosso-bg); color: var(--rosso); }
.badge.info { background: var(--accent-soft); color: var(--accent); }

/* Detail row */
tr.detail td { background: var(--detail-bg); white-space: normal; max-width: none; padding: 14px 18px; }
.detail-grid { display: grid; grid-template-columns: 300px 1fr; gap: 18px; }
@media (max-width: 900px) { .detail-grid { grid-template-columns: 1fr; } }
.detail-imgs img { width: 100%; border-radius: 8px; border: 1px solid var(--line); margin-bottom: 8px; background: var(--img-bg); }
.detail-imgs .ph { color: var(--muted); font-size: 12px; padding: 16px; border: 1px dashed var(--line); border-radius: 8px; text-align: center; }
.detail h4 { margin: 0 0 6px; font-size: 13px; }
.detail .desc { color: var(--desc-ink); font-size: 12.5px; max-height: 180px; overflow-y: auto; margin-bottom: 12px; }
.cost-table { border: 1px solid var(--line); border-radius: 8px; overflow: hidden; margin-bottom: 12px; max-width: 480px; }
.cost-table div { display: flex; justify-content: space-between; padding: 5px 12px; border-bottom: 1px solid var(--line); font-size: 12.5px; }
.cost-table div:last-child { border-bottom: 0; }
.cost-table div.tot { background: var(--tot-bg); font-weight: 700; }
.cost-table .neg { color: var(--rosso); }
.cost-table .pos { color: var(--verde); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 10px; }
.motiv { color: var(--muted); font-size: 12px; }

/* Mobile: sidebar come drawer a scomparsa */
@media (max-width: 760px) {
  .sidebar { position: fixed; top: 0; left: 0; height: 100vh; width: min(280px, 84vw); flex: none; z-index: 60; transform: translateX(-100%); transition: transform .25s ease; box-shadow: 0 0 40px rgba(0,0,0,.4); }
  body.nav-open .sidebar { transform: translateX(0); }
  body.nav-open { overflow: hidden; }
  .nav-backdrop { display: block; position: fixed; inset: 0; background: rgba(10,16,28,.5); z-index: 55; opacity: 0; pointer-events: none; transition: opacity .25s ease; }
  body.nav-open .nav-backdrop { opacity: 1; pointer-events: auto; }
  .nav-toggle { display: inline-flex; }
  .main { padding: 14px 14px 32px; }
  .topbar { gap: 10px; }
  .topbar h1 { font-size: 18px; }
  .controls { width: 100%; }
  .controls input { width: 100%; flex: 1 1 100%; }
  .controls select { flex: 1 1 auto; min-width: 0; }
  .kpis { grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 10px; }
  .kpi .v { font-size: 22px; }
  .table-scroll { max-height: calc(100vh - 210px); }
  .detail-grid { grid-template-columns: 1fr; }
  .cost-table { max-width: 100%; }
  .mini-row { flex-wrap: wrap; }
  .mini-row .v { white-space: normal; text-align: right; max-width: 100%; }
}
/* Chat assistente (pannello stile Rufus) */
#btn-chat { font-size: 16px; line-height: 1; padding: 7px 12px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel); cursor: pointer; align-self: flex-start; }
#btn-chat:hover { background: var(--accent-soft); border-color: var(--accent); }
#btn-chat.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.chat-panel { position: fixed; top: 0; right: 0; height: 100vh; width: var(--chat-w, 380px); min-width: 300px; max-width: 94vw; background: var(--panel); border-left: 1px solid var(--line); box-shadow: -12px 0 32px rgba(16,24,40,.12); display: flex; flex-direction: column; z-index: 70; transform: translateX(102%); transition: transform .25s ease; }
body.chat-resizing .chat-panel { transition: none; }
.chat-resizer { position: absolute; top: 0; left: -4px; width: 8px; height: 100%; cursor: ew-resize; z-index: 5; touch-action: none; }
.chat-resizer:hover, .chat-resizer:focus-visible, body.chat-resizing .chat-resizer { background: var(--accent-soft); border-left: 2px solid var(--accent); outline: none; }
body.chat-resizing { user-select: none; cursor: ew-resize; }
body.chat-open .chat-panel { transform: translateX(0); }
.chat-backdrop { display: none; }
.chat-head { display: flex; align-items: center; gap: 8px; padding: 12px 14px; border-bottom: 1px solid var(--line); }
.chat-title { font-weight: 700; }
.chat-model { flex: 1; font-size: 11px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#chat-close { border: 0; background: none; font-size: 15px; cursor: pointer; color: var(--muted); padding: 4px 6px; border-radius: 6px; }
#chat-close:hover { background: var(--accent-soft); color: var(--accent); }
.chat-log { flex: 1; overflow-y: auto; padding: 12px 14px; display: flex; flex-direction: column; gap: 10px; }
.chat-msg { max-width: 92%; padding: 8px 11px; border-radius: 12px; font-size: 13px; white-space: pre-wrap; overflow-wrap: anywhere; }
.chat-msg.user { align-self: flex-end; background: var(--accent); color: #fff; border-bottom-right-radius: 4px; }
.chat-msg.bot { align-self: flex-start; background: var(--bg); border: 1px solid var(--line); border-bottom-left-radius: 4px; }
.chat-msg.err { align-self: flex-start; background: var(--rosso-bg); color: var(--rosso); border: 1px solid var(--err-line); }
.chat-msg.wait { color: var(--muted); font-style: italic; }
.chat-action { align-self: flex-start; font-size: 11px; color: var(--accent); background: var(--accent-soft); border-radius: 8px; padding: 3px 9px; cursor: pointer; border: 0; font-family: inherit; text-align: left; }
.chat-action:hover { text-decoration: underline; }
.chat-cards { align-self: stretch; display: flex; flex-direction: column; gap: 6px; }
.chat-card { border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; font-size: 12px; cursor: pointer; background: var(--panel); }
.chat-card:hover { border-color: var(--accent); background: var(--accent-soft); }
.chat-card .t { font-weight: 600; display: flex; justify-content: space-between; gap: 8px; }
.chat-card .t .p { white-space: nowrap; }
.chat-card .d { color: var(--muted); margin-top: 2px; }
.chat-card .m { margin-top: 4px; display: flex; flex-wrap: wrap; gap: 4px; }
.chat-chips { display: flex; flex-wrap: wrap; gap: 6px; padding: 0 14px 8px; }
.chat-chips button { font-size: 11.5px; border: 1px solid var(--line); background: var(--bg); color: var(--ink); border-radius: 999px; padding: 4px 10px; cursor: pointer; font-family: inherit; }
.chat-chips button:hover { border-color: var(--accent); background: var(--accent-soft); }
.chat-form { display: flex; gap: 8px; padding: 10px 14px 14px; border-top: 1px solid var(--line); }
.chat-form input { flex: 1; padding: 9px 11px; border: 1px solid var(--line); border-radius: 10px; font: inherit; color: var(--ink); background: var(--bg); min-width: 0; }
.chat-form input:focus { outline: none; border-color: var(--accent); }
.chat-form button { border: 0; background: var(--accent); color: #fff; border-radius: 10px; width: 42px; cursor: pointer; font-size: 15px; }
.chat-form button:disabled { opacity: .5; cursor: default; }
.chat-form button.stop { background: var(--rosso); }
.chat-form button.stop:hover { filter: brightness(1.15); }
@media (max-width: 760px) {
  .chat-panel { width: 100vw; max-width: 100vw; min-width: 0; }
  .chat-resizer { display: none; }
  .chat-backdrop { display: block; position: fixed; inset: 0; background: rgba(10,16,28,.5); z-index: 65; opacity: 0; pointer-events: none; transition: opacity .25s ease; }
  body.chat-open .chat-backdrop { opacity: 1; pointer-events: auto; }
  body.chat-open { overflow: hidden; }
}
