/* =====================================================
   Risk Solution · Archivio S3 — tema light moderno
   ===================================================== */
:root {
  --rs-bg: #f5f7fb;
  --rs-surface: #ffffff;
  --rs-border: #e6e9f0;
  --rs-text: #1f2937;
  --rs-muted: #6b7280;
  --rs-primary: #4f46e5;
  --rs-primary-soft: #eef2ff;
  --rs-cyan: #06b6d4;
  --rs-emerald: #10b981;
  --rs-amber: #f59e0b;
  --rs-rose: #f43f5e;
  --rs-radius: 14px;
  --rs-shadow: 0 1px 2px rgba(16, 24, 40, .04), 0 4px 16px rgba(16, 24, 40, .06);
  --sidebar-w: 256px;
  --bs-body-font-family: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --bs-primary: var(--rs-primary);
  --bs-primary-rgb: 79, 70, 229;
  --bs-link-color: var(--rs-primary);
  --bs-link-color-rgb: 79, 70, 229;
  --bs-link-hover-color: #4338ca;
  --bs-body-bg: var(--rs-bg);
  --bs-body-color: var(--rs-text);
  --bs-border-color: var(--rs-border);
}
html, body { height: 100%; }
body { background: var(--rs-bg); color: var(--rs-text); -webkit-font-smoothing: antialiased; }
.btn-primary { --bs-btn-bg: var(--rs-primary); --bs-btn-border-color: var(--rs-primary); --bs-btn-hover-bg: #4338ca; --bs-btn-hover-border-color: #4338ca; --bs-btn-active-bg: #3730a3; --bs-btn-active-border-color: #3730a3; }
.btn-outline-primary { --bs-btn-color: var(--rs-primary); --bs-btn-border-color: #c7d2fe; --bs-btn-hover-bg: var(--rs-primary); --bs-btn-hover-border-color: var(--rs-primary); }
.btn-light { --bs-btn-bg: #fff; --bs-btn-border-color: var(--rs-border); --bs-btn-hover-bg: #f3f4f6; --bs-btn-hover-border-color: #d1d5db; }
.form-control, .form-select { border-color: var(--rs-border); border-radius: 10px; }
.form-control:focus, .form-select:focus { border-color: #a5b4fc; box-shadow: 0 0 0 .2rem rgba(79, 70, 229, .15); }
.input-group-text { background: #fafbfd; border-color: var(--rs-border); border-radius: 10px; color: var(--rs-muted); }
.card { border: 1px solid var(--rs-border); border-radius: var(--rs-radius); box-shadow: var(--rs-shadow); background: var(--rs-surface); }
.card-header { background: transparent; border-bottom: 1px solid var(--rs-border); font-weight: 600; padding: .85rem 1.1rem; display: flex; align-items: center; }
.card-header i { color: var(--rs-primary); }
.card-footer { background: #fafbfd; border-top: 1px solid var(--rs-border); }
.badge { font-weight: 500; }
.table { --bs-table-hover-bg: #f8f9fe; }
.table > :not(caption) > * > * { padding: .65rem .9rem; vertical-align: middle; }
.table thead th { font-size: .75rem; text-transform: uppercase; letter-spacing: .04em; color: var(--rs-muted); font-weight: 600; border-bottom: 1px solid var(--rs-border); background: #fafbfd; white-space: nowrap; }
th[data-sort] { cursor: pointer; user-select: none; }
th[data-sort]:hover { color: var(--rs-primary); }
th[data-sort].active::after { content: " ↓"; }
th[data-sort].active.asc::after { content: " ↑"; }

/* Brand */
.brand-mark { width: 56px; height: 56px; border-radius: 16px; display: inline-flex; align-items: center; justify-content: center; font-size: 26px; color: #fff; background: linear-gradient(135deg, #4f46e5, #06b6d4); box-shadow: 0 8px 20px rgba(79, 70, 229, .25); }
.brand-mark.sm { width: 38px; height: 38px; border-radius: 11px; font-size: 18px; box-shadow: none; }

/* Login */
.login-body { display: flex; min-height: 100%; background: radial-gradient(1200px 600px at 10% -10%, #e0e7ff 0%, transparent 60%), radial-gradient(900px 500px at 100% 100%, #cffafe 0%, transparent 55%), var(--rs-bg); }
.login-wrap { flex: 1; display: flex; align-items: center; justify-content: center; padding: 24px; }
.login-card { width: 100%; max-width: 420px; border-radius: 20px; }
.toggle-pw { position: absolute; right: 6px; top: 50%; transform: translateY(-50%); color: var(--rs-muted); text-decoration: none; }

/* Layout */
.app-shell { display: flex; min-height: 100vh; }
.sidebar { width: var(--sidebar-w); background: var(--rs-surface); border-right: 1px solid var(--rs-border); display: flex; flex-direction: column; position: fixed; inset: 0 auto 0 0; z-index: 1045; }
@media (min-width: 992px) { .sidebar { position: sticky; top: 0; height: 100vh; transform: none !important; visibility: visible !important; } }
.sidebar-brand { display: flex; align-items: center; gap: 10px; padding: 18px 18px 14px; }
.sidebar-nav { padding: 6px 12px; flex: 1; overflow-y: auto; }
.nav-label { font-size: .7rem; text-transform: uppercase; letter-spacing: .08em; color: var(--rs-muted); padding: 14px 12px 6px; font-weight: 600; }
.sidebar-nav .nav-item { display: flex; align-items: center; gap: 12px; padding: 10px 12px; border-radius: 10px; color: #374151; text-decoration: none; font-weight: 500; margin-bottom: 2px; transition: background .15s, color .15s; }
.sidebar-nav .nav-item i { font-size: 1.1rem; color: var(--rs-muted); width: 20px; text-align: center; }
.sidebar-nav .nav-item:hover { background: #f3f4f6; }
.sidebar-nav .nav-item.active { background: var(--rs-primary-soft); color: var(--rs-primary); }
.sidebar-nav .nav-item.active i { color: var(--rs-primary); }
.sidebar-nav .nav-item .badge { background: #e0e7ff; color: var(--rs-primary); font-size: .6rem; }
.sidebar-footer { padding: 14px; border-top: 1px solid var(--rs-border); }
.avatar { width: 36px; height: 36px; border-radius: 50%; background: linear-gradient(135deg, #4f46e5, #06b6d4); color: #fff; display: inline-flex; align-items: center; justify-content: center; font-weight: 600; flex-shrink: 0; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar { position: sticky; top: 0; z-index: 1020; display: flex; align-items: center; gap: 12px; padding: 12px 20px; background: rgba(245, 247, 251, .85); backdrop-filter: blur(8px); border-bottom: 1px solid var(--rs-border); }
.topbar-search { position: relative; flex: 1; max-width: 640px; }
.topbar-search i { position: absolute; left: 14px; top: 50%; transform: translateY(-50%); color: var(--rs-muted); }
.topbar-search .form-control { padding-left: 40px; background: #fff; border-radius: 12px; height: 42px; }
.net-badge { width: 34px; height: 34px; border-radius: 10px; display: inline-flex; align-items: center; justify-content: center; background: #ecfdf5; color: #059669; border: 1px solid #d1fae5; }
.net-badge.offline { background: #fff1f2; color: #e11d48; border-color: #fecdd3; }
.content { padding: 20px; max-width: 1500px; width: 100%; }
@media (max-width: 575px) { .content { padding: 14px; } .topbar { padding: 10px 12px; } }
.section-head { display: flex; align-items: center; gap: 12px; margin-bottom: 18px; flex-wrap: wrap; }

/* Stat tiles */
.stat-tile { background: var(--rs-surface); border: 1px solid var(--rs-border); border-radius: var(--rs-radius); box-shadow: var(--rs-shadow); padding: 16px 18px; display: flex; align-items: center; gap: 14px; height: 100%; }
.stat-icon { width: 46px; height: 46px; border-radius: 12px; display: inline-flex; align-items: center; justify-content: center; font-size: 1.3rem; color: #fff; flex-shrink: 0; }
.bg-indigo { background: linear-gradient(135deg, #6366f1, #4f46e5); }
.bg-cyan { background: linear-gradient(135deg, #22d3ee, #06b6d4); }
.bg-emerald { background: linear-gradient(135deg, #34d399, #10b981); }
.bg-amber { background: linear-gradient(135deg, #fbbf24, #f59e0b); }
.stat-value { font-size: 1.45rem; font-weight: 700; line-height: 1.15; }
.stat-label { font-size: .78rem; color: var(--rs-muted); }

/* File icons */
.file-icon { width: 34px; height: 34px; border-radius: 9px; display: inline-flex; align-items: center; justify-content: center; font-size: 1.05rem; background: #f3f4f6; color: #4b5563; flex-shrink: 0; }
.file-icon.ext-pdf { background: #fee2e2; color: #dc2626; }
.file-icon.ext-xls { background: #dcfce7; color: #16a34a; }
.file-icon.ext-doc { background: #dbeafe; color: #2563eb; }
.file-icon.ext-ppt { background: #ffedd5; color: #ea580c; }
.file-icon.ext-img { background: #f3e8ff; color: #9333ea; }
.file-icon.ext-zip { background: #fef3c7; color: #d97706; }
.file-icon.ext-cad { background: #ccfbf1; color: #0d9488; }
.file-icon.ext-txt { background: #e5e7eb; color: #374151; }
.file-icon.ext-mail { background: #e0f2fe; color: #0284c7; }
.files-table .fname { font-weight: 500; cursor: pointer; }
.files-table .fname:hover { color: var(--rs-primary); }
.files-table .fdir { color: var(--rs-muted); font-size: .8rem; max-width: 340px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.files-table td.nowrap { white-space: nowrap; }
.files-table .row-actions .btn { padding: .2rem .45rem; }
.pagination { --bs-pagination-active-bg: var(--rs-primary); --bs-pagination-active-border-color: var(--rs-primary); --bs-pagination-color: var(--rs-text); }
.status-badge { font-size: .7rem; padding: .25em .55em; border-radius: 6px; font-weight: 600; }
.status-uploaded { background: #dcfce7; color: #15803d; }
.status-error { background: #fee2e2; color: #b91c1c; }
.status-nodb { background: #f3f4f6; color: #6b7280; }
.status-running { background: #fef3c7; color: #b45309; }
.status-ok { background: #dcfce7; color: #15803d; }
.status-stale { background: #ffe4e6; color: #be123c; }

/* Tree */
.tree-card { max-height: calc(100vh - 170px); display: flex; flex-direction: column; }
.tree { overflow: auto; padding: 8px 6px; font-size: .9rem; flex: 1; }
.tree ul { list-style: none; margin: 0; padding-left: 16px; }
.tree ul.tree-root { padding-left: 0; }
.tree li { margin: 1px 0; }
.tree .node { display: flex; align-items: center; gap: 4px; padding: 4px 6px; border-radius: 8px; cursor: pointer; white-space: nowrap; }
.tree .node:hover { background: #f3f4f6; }
.tree .node.selected { background: var(--rs-primary-soft); color: var(--rs-primary); }
.tree .node .caret { width: 16px; display: inline-flex; justify-content: center; color: var(--rs-muted); font-size: .75rem; }
.tree .node .caret.leaf { visibility: hidden; }
.tree .node .bi-folder-fill, .tree .node .bi-folder2-open { color: #f59e0b; }
.tree .node .cnt { margin-left: auto; font-size: .72rem; color: var(--rs-muted); background: #f3f4f6; border-radius: 999px; padding: 0 7px; }
.tree .node.selected .cnt { background: #e0e7ff; color: var(--rs-primary); }
.tree .node .lbl { overflow: hidden; text-overflow: ellipsis; }
.folder-crumbs { background: #fff; border: 1px solid var(--rs-border); border-radius: 10px; padding: 8px 14px; }
.folder-crumbs a { text-decoration: none; }
.folders-table td .name { cursor: pointer; font-weight: 500; }
.folders-table td .name:hover { color: var(--rs-primary); }

/* Detail offcanvas */
.file-detail { width: min(520px, 100%); }
.file-detail .kv { display: grid; grid-template-columns: 130px 1fr; gap: 6px 12px; font-size: .88rem; }
.file-detail .kv dt { color: var(--rs-muted); font-weight: 500; }
.file-detail .kv dd { margin: 0; word-break: break-all; }
.file-detail .actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }

/* Chat */
.chat-card { display: flex; flex-direction: column; height: calc(100vh - 180px); min-height: 460px; }
.chat-messages { flex: 1; overflow-y: auto; padding: 20px; display: flex; flex-direction: column; gap: 14px; }
.chat-empty { margin: auto; text-align: center; max-width: 640px; }
.chat-suggestions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.chip { border: 1px solid var(--rs-border); background: #fff; border-radius: 999px; padding: 6px 14px; font-size: .85rem; color: #374151; cursor: pointer; transition: all .15s; }
.chip:hover { border-color: #a5b4fc; color: var(--rs-primary); background: var(--rs-primary-soft); }
.msg { display: flex; gap: 10px; max-width: 86%; }
.msg.user { align-self: flex-end; flex-direction: row-reverse; }
.msg .bubble { padding: 10px 14px; border-radius: 16px; line-height: 1.5; font-size: .93rem; }
.msg.user .bubble { background: var(--rs-primary); color: #fff; border-bottom-right-radius: 4px; }
.msg.bot .bubble { background: #f3f4f6; border-bottom-left-radius: 4px; }
.msg .bubble p:last-child { margin-bottom: 0; }
.msg .bubble ul, .msg .bubble ol { padding-left: 20px; margin-bottom: .4rem; }
.msg .bubble a { font-weight: 500; }
.msg .avatar { width: 30px; height: 30px; font-size: .8rem; }
.msg.bot .avatar { background: linear-gradient(135deg, #f59e0b, #f43f5e); }
.msg .tools { font-size: .7rem; color: var(--rs-muted); margin-top: 6px; display: flex; flex-wrap: wrap; gap: 4px; }
.msg .tools span { background: #fff; border: 1px solid var(--rs-border); border-radius: 6px; padding: 1px 6px; }
.chat-input { display: flex; gap: 8px; padding: 12px; border-top: 1px solid var(--rs-border); background: #fafbfd; border-radius: 0 0 var(--rs-radius) var(--rs-radius); }
.chat-input textarea { resize: none; max-height: 140px; }
.typing span { display: inline-block; width: 6px; height: 6px; border-radius: 50%; background: #9ca3af; margin-right: 3px; animation: blink 1.2s infinite; }
.typing span:nth-child(2) { animation-delay: .2s; } .typing span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .2 } 40% { opacity: 1 } }

/* Misc */
.skeleton { background: linear-gradient(90deg, #f3f4f6 25%, #e5e7eb 37%, #f3f4f6 63%); background-size: 400% 100%; animation: sk 1.4s ease infinite; border-radius: 6px; color: transparent !important; }
@keyframes sk { 0% { background-position: 100% 50% } 100% { background-position: 0 50% } }
.list-group-item { border-color: var(--rs-border); }
.list-group-item-action { cursor: pointer; }
.progress { height: 6px; }
.text-mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .8rem; }
.toast { border-radius: 12px; }
.run-tile { border: 1px solid var(--rs-border); border-radius: 12px; padding: 12px 14px; background: #fafbfd; height: 100%; }
.run-tile .t { font-size: .72rem; text-transform: uppercase; letter-spacing: .05em; color: var(--rs-muted); font-weight: 600; }
.run-tile .v { font-weight: 600; }
kbd { background: #f3f4f6; color: #374151; border: 1px solid #e5e7eb; border-radius: 5px; padding: 1px 5px; font-size: .75rem; }

/* =====================================================
   Restyling: topbar, bottom-nav mobile, anteprima, rifiniture
   ===================================================== */
.topbar { background: #fff; }
.btn { border-radius: 10px; }
.card, .stat-tile { transition: box-shadow .18s ease, transform .18s ease; }
.stat-tile:hover { transform: translateY(-2px); box-shadow: 0 8px 24px rgba(16, 24, 40, .09); }
.sidebar-nav .nav-item { position: relative; }
.sidebar-nav .nav-item.active::before { content: ""; position: absolute; left: -12px; top: 9px; bottom: 9px; width: 3px; border-radius: 3px; background: var(--rs-primary); }
.section { animation: fadein .2s ease; }
@keyframes fadein { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }
.list-group-item-action:hover { background: #f8f9fe; }
.files-table td { font-size: .9rem; }
.row-actions .btn { border-radius: 8px; }
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: #d6dae3; border-radius: 8px; border: 2px solid #f5f7fb; }
::-webkit-scrollbar-thumb:hover { background: #b9bfcc; }

/* Barra di navigazione mobile */
.bottom-nav { position: fixed; bottom: 0; left: 0; right: 0; z-index: 1030; display: flex; background: rgba(255, 255, 255, .95); backdrop-filter: blur(12px); border-top: 1px solid var(--rs-border); padding: 6px max(10px, env(safe-area-inset-right)) calc(6px + env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left)); box-shadow: 0 -4px 16px rgba(16, 24, 40, .06); }
.bottom-nav a { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 3px; padding: 6px 4px; text-decoration: none; color: var(--rs-muted); font-size: .68rem; font-weight: 500; border-radius: 12px; transition: color .15s; }
.bottom-nav a i { font-size: 1.3rem; line-height: 1; }
.bottom-nav a.active { color: var(--rs-primary); }
.bottom-nav a:active { background: var(--rs-primary-soft); }
@media (max-width: 991.98px) {
  .content { padding-bottom: 92px; }
  .chat-card { height: calc(100dvh - 235px); min-height: 380px; }
  .file-detail { width: 100%; }
}
@media (max-width: 575.98px) {
  .files-table .fname { max-width: 48vw !important; }
  .table > :not(caption) > * > * { padding: .55rem .6rem; }
  .stat-value { font-size: 1.12rem; }
  .section-head h1 { font-size: 1.15rem; }
  .stat-tile { padding: 12px; gap: 10px; }
  .stat-icon { width: 40px; height: 40px; font-size: 1.1rem; }
  .row-actions .btn { padding: .35rem .55rem; }
}

/* Anteprima */
.preview-content { border-radius: var(--rs-radius); overflow: hidden; }
.modal-xl .preview-content { height: calc(100vh - 60px); }
.preview-body { background: #eef0f6; display: flex; flex-direction: column; flex: 1; overflow: auto; }
.preview-body iframe { border: 0; width: 100%; flex: 1; min-height: 60vh; background: #fff; }
.pv-center { margin: auto; display: flex; align-items: center; justify-content: center; width: 100%; padding: 20px; flex: 1; }
.pv-center .file-icon { width: 64px; height: 64px; font-size: 2rem; border-radius: 16px; }
.pv-dark { background: #1d2230; }
.pv-center img { max-width: 100%; max-height: calc(100vh - 170px); border-radius: 8px; box-shadow: 0 8px 30px rgba(0, 0, 0, .35); }
.pv-text { width: 100%; margin: 0; padding: 18px; background: #fff; font-size: .82rem; overflow: auto; flex: 1; }
@media (max-width: 991.98px) {
  .preview-body iframe { min-height: calc(100dvh - 64px); }
  .pv-center img { max-height: calc(100dvh - 130px); }
  .modal-fullscreen-lg-down .preview-content { border-radius: 0; height: 100%; }
}

/* Home archivio: 3 numeri compatti + albero subito visibile */
#stat-tiles .stat-tile { padding: 12px 14px; }
@media (max-width: 767.98px) {
  #stat-tiles .stat-icon { display: none; }
  #stat-tiles .stat-tile { justify-content: center; text-align: center; padding: 10px 6px; }
  #stat-tiles .stat-value { font-size: 1.05rem; }
  #stat-tiles .stat-label { font-size: .68rem; }
}
@media (max-width: 991.98px) {
  .tree-card { max-height: 46vh; }
}

/* Visualizzatori anteprima in-app (Excel / Word / PDF.js) */
.pv-sheet { display: flex; flex-direction: column; flex: 1; min-height: 0; background: #fff; }
.sheet-tabs { display: flex; gap: 6px; padding: 8px 12px; border-bottom: 1px solid var(--rs-border); overflow-x: auto; background: #fafbfd; flex-shrink: 0; }
.sheet-tab { border: 1px solid var(--rs-border); background: #fff; border-radius: 8px; padding: 4px 12px; font-size: .8rem; white-space: nowrap; cursor: pointer; color: #374151; }
.sheet-tab.active { background: var(--rs-primary); color: #fff; border-color: var(--rs-primary); }
.sheet-table { overflow: auto; flex: 1; }
.sheet-table table { border-collapse: collapse; font-size: .82rem; min-width: 100%; background: #fff; }
.sheet-table td { border: 1px solid #e8eaf0; padding: 4px 9px; white-space: nowrap; max-width: 360px; overflow: hidden; text-overflow: ellipsis; }
.sheet-table tr:first-child td { position: sticky; top: 0; background: #f3f4f6; font-weight: 600; z-index: 1; box-shadow: 0 1px 0 var(--rs-border); }
.sheet-note { border-top: 1px solid var(--rs-border); background: #fffbeb; flex-shrink: 0; }
.pv-doc { background: #fff; overflow: auto; flex: 1; padding: 28px clamp(16px, 7vw, 90px); line-height: 1.65; }
.pv-doc img { max-width: 100%; }
.pv-doc table { border-collapse: collapse; margin: 1rem 0; }
.pv-doc table td, .pv-doc table th { border: 1px solid #e8eaf0; padding: 4px 9px; }
.pv-pdf { overflow: auto; flex: 1; padding: 14px 10px; display: flex; flex-direction: column; align-items: center; gap: 12px; background: #444a5c; }
.pv-pdf canvas { width: 100%; max-width: 900px; height: auto; border-radius: 4px; box-shadow: 0 4px 18px rgba(0, 0, 0, .35); background: #fff; }

/* Pannello "AI su questo file" nell'anteprima */
.pv-ai-panel { border-top: 1px solid var(--rs-border); background: #fff; display: flex; flex-direction: column; height: 42%; min-height: 250px; flex-shrink: 0; }
.pv-ai-head { display: flex; gap: 8px; align-items: center; padding: 8px 12px; border-bottom: 1px solid var(--rs-border); font-weight: 600; font-size: .88rem; background: #fafbfd; }
.pv-ai-msgs { flex: 1; overflow-y: auto; padding: 10px 14px; display: flex; flex-direction: column; gap: 8px; }
.pv-ai-msgs .msg { max-width: 94%; }
.pv-ai-msgs .msg.user { align-self: flex-end; }
.pv-ai-msgs .msg .bubble { font-size: .86rem; padding: 8px 12px; }
.pv-ai-chips { display: flex; gap: 6px; padding: 0 12px 8px; overflow-x: auto; flex-shrink: 0; }
.pv-ai-chips .chip { font-size: .78rem; padding: 4px 12px; white-space: nowrap; }
.pv-ai-form { display: flex; gap: 8px; padding: 8px 12px; border-top: 1px solid var(--rs-border); flex-shrink: 0; }
@media (max-width: 991.98px) { .pv-ai-panel { height: 52%; } }
