:root {
  --bg: #f6f4f0;
  --panel: #ffffff;
  --panel-2: #faf8f5;
  --line: #e6e1d8;
  --text: #24211d;
  --muted: #6f685e;
  --accent: #b5651d;
  --accent-soft: #f6e8da;
  --ok: #2e7d4f;
  --ok-soft: #e3f2e8;
  --warn: #b07a00;
  --warn-soft: #fbf0d4;
  --bad: #b3362d;
  --bad-soft: #f9e2df;
  --blue: #2f64b5;
  --blue-soft: #e2ebf8;
  --violet: #7449b0;
  --violet-soft: #ece4f7;
  --shadow: 0 1px 2px rgba(40, 30, 20, .06), 0 2px 8px rgba(40, 30, 20, .04);
  --radius: 12px;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161412;
    --panel: #1f1c19;
    --panel-2: #252220;
    --line: #35302b;
    --text: #ece7e0;
    --muted: #a39b90;
    --accent: #e0914a;
    --accent-soft: #3a2a1c;
    --ok: #5cc28a;
    --ok-soft: #1c3226;
    --warn: #e2b44a;
    --warn-soft: #3a3019;
    --bad: #ef7b70;
    --bad-soft: #3d201d;
    --blue: #7ea6ea;
    --blue-soft: #1d2a3f;
    --violet: #b496e6;
    --violet-soft: #2b2340;
    --shadow: none;
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--text);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
button { font: inherit; color: inherit; }

/* layout */
.app { display: grid; grid-template-columns: 220px minmax(0, 1fr) 380px; height: 100vh; }
.app.chat-closed { grid-template-columns: 220px minmax(0, 1fr) 0; }
.side { border-right: 1px solid var(--line); background: var(--panel); display: flex; flex-direction: column; overflow-y: auto; }
.brand { display: flex; align-items: center; gap: 10px; padding: 18px 18px 12px; font-weight: 700; font-size: 17px; }
.brand .bear { font-size: 24px; }
.brand small { display: block; font-weight: 400; color: var(--muted); font-size: 12px; }
.nav { padding: 6px 10px 20px; display: flex; flex-direction: column; gap: 2px; }
.nav a { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 10px; border-radius: 8px; color: var(--text); font-size: 14px; }
.nav a:hover { background: var(--panel-2); text-decoration: none; }
.nav a.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.nav .count { font-size: 12px; background: var(--panel-2); color: var(--muted); border-radius: 20px; padding: 0 7px; }
.nav a.active .count { background: var(--panel); }
.nav .count.hot { background: var(--bad-soft); color: var(--bad); font-weight: 600; }
.side-foot { margin-top: auto; padding: 14px 18px; font-size: 12px; color: var(--muted); border-top: 1px solid var(--line); }
.side-foot button { background: none; border: 0; color: var(--muted); padding: 0; cursor: pointer; text-decoration: underline; }

.main { overflow-y: auto; min-width: 0; }
.topbar { position: sticky; top: 0; z-index: 5; display: flex; align-items: center; gap: 12px; padding: 12px 28px; background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(8px); border-bottom: 1px solid var(--line); }
.topbar h1 { font-size: 18px; margin: 0; flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.search { width: 260px; max-width: 40vw; padding: 7px 12px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel); color: var(--text); }
.iconbtn { border: 1px solid var(--line); background: var(--panel); border-radius: 8px; padding: 6px 10px; cursor: pointer; white-space: nowrap; }
.iconbtn:hover { border-color: var(--accent); }
.menu-btn { display: none; }
.content { padding: 22px 28px 60px; max-width: 1180px; }

/* cards */
.grid { display: grid; gap: 14px; }
.kpis { grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px 18px; box-shadow: var(--shadow); min-width: 0; }
.card h2, .card h3 { margin: 0 0 10px; font-size: 15px; }
.card h2 { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.kpi .label { color: var(--muted); font-size: 12.5px; }
.kpi .value { font-size: 26px; font-weight: 700; letter-spacing: -.02em; margin-top: 2px; font-variant-numeric: tabular-nums; }
.kpi .sub { color: var(--muted); font-size: 12px; margin-top: 2px; }
.kpi.bad .value { color: var(--bad); }
.kpi.ok .value { color: var(--ok); }
.section { margin-top: 22px; }
.section > h2 { font-size: 15px; margin: 0 0 10px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; font-size: 12.5px; }
.two { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.mono { font-family: var(--mono); font-size: 12.5px; }
.row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.spacer { flex: 1; }
.empty { color: var(--muted); padding: 18px; text-align: center; border: 1px dashed var(--line); border-radius: var(--radius); }

/* badges */
.badge { display: inline-flex; align-items: center; gap: 5px; font-size: 12px; font-weight: 600; padding: 2px 9px; border-radius: 20px; background: var(--panel-2); color: var(--muted); white-space: nowrap; }
.badge.idea { background: var(--panel-2); color: var(--muted); }
.badge.ricerca { background: var(--blue-soft); color: var(--blue); }
.badge.costruzione { background: var(--violet-soft); color: var(--violet); }
.badge.test-vivo { background: var(--ok-soft); color: var(--ok); }
.badge.in-vendita { background: var(--warn-soft); color: var(--warn); }
.badge.chiuso { background: var(--panel-2); color: var(--muted); text-decoration: line-through; }
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.bad { background: var(--bad-soft); color: var(--bad); }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); display: inline-block; }
.dot.ok { background: var(--ok); } .dot.bad { background: var(--bad); } .dot.warn { background: var(--warn); }

/* stage funnel */
.funnel { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 8px; }
.funnel .step { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; }
.funnel .step b { display: block; font-size: 22px; }
.funnel .step span { color: var(--muted); font-size: 12px; }
.funnel .step.on { border-color: var(--ok); }

/* project cards */
.projects { grid-template-columns: repeat(auto-fill, minmax(290px, 1fr)); }
.pcard { cursor: pointer; display: flex; flex-direction: column; gap: 8px; transition: border-color .15s; }
.pcard:hover { border-color: var(--accent); }
.pcard .title { font-weight: 650; font-size: 15.5px; }
.pcard .desc { color: var(--muted); font-size: 13.5px; display: -webkit-box; -webkit-line-clamp: 4; -webkit-box-orient: vertical; overflow: hidden; }
.pcard.closed { opacity: .65; }
.kv { display: grid; grid-template-columns: max-content 1fr; gap: 4px 12px; font-size: 13.5px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }

/* tabs */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin: 16px 0 0; overflow-x: auto; }
.tabs button { border: 0; background: none; padding: 9px 12px; cursor: pointer; color: var(--muted); border-bottom: 2px solid transparent; white-space: nowrap; font-size: 14px; }
.tabs button.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }

/* lists */
.list { display: flex; flex-direction: column; }
.list .item { padding: 11px 2px; border-bottom: 1px solid var(--line); min-width: 0; }
.list .item:last-child { border-bottom: 0; }
.list .item .head { display: flex; gap: 8px; align-items: baseline; flex-wrap: wrap; }
.list .item .head b { font-weight: 600; }
details.acc { border: 1px solid var(--line); border-radius: 10px; background: var(--panel); margin-bottom: 8px; }
details.acc > summary { cursor: pointer; padding: 11px 14px; display: flex; gap: 8px; align-items: center; list-style: none; }
details.acc > summary::-webkit-details-marker { display: none; }
details.acc > summary::before { content: "▸"; color: var(--muted); transition: transform .15s; }
details.acc[open] > summary::before { transform: rotate(90deg); }
details.acc > .body { padding: 0 16px 14px; }

/* tables */
.tablewrap { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; font-size: 13.5px; }
th, td { text-align: left; padding: 7px 9px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { color: var(--muted); font-weight: 600; font-size: 12.5px; background: var(--panel-2); position: sticky; top: 0; }
tr:hover td { background: var(--panel-2); }

/* markdown */
.md { overflow-wrap: anywhere; }
.md h1 { font-size: 20px; } .md h2 { font-size: 17px; margin-top: 22px; } .md h3 { font-size: 15px; }
.md table { margin: 10px 0; display: block; overflow-x: auto; }
.md code { font-family: var(--mono); font-size: 12.5px; background: var(--panel-2); padding: 1px 5px; border-radius: 5px; }
.md pre { background: var(--panel-2); padding: 12px; border-radius: 8px; overflow-x: auto; }
.md pre code { background: none; padding: 0; }
.md blockquote { margin: 8px 0; padding: 4px 14px; border-left: 3px solid var(--accent); color: var(--muted); }
.md p, .md li { max-width: 86ch; }
.md img { max-width: 100%; }

/* timeline */
.timeline .t { display: grid; grid-template-columns: 120px minmax(0, 1fr); gap: 12px; padding: 12px 0; border-bottom: 1px solid var(--line); }
.timeline .t .when { color: var(--muted); font-size: 12.5px; }

/* chat */
.chat { border-left: 1px solid var(--line); background: var(--panel); display: flex; flex-direction: column; min-width: 0; overflow: hidden; }
.app.chat-closed .chat { display: none; }
.chat-head { padding: 12px 14px; border-bottom: 1px solid var(--line); display: flex; align-items: center; gap: 8px; }
.chat-head .ctx { flex: 1; min-width: 0; }
.chat-head .ctx b { display: block; font-size: 14px; }
.chat-head .ctx span { display: block; font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-head select { max-width: 130px; border: 1px solid var(--line); border-radius: 6px; background: var(--panel); color: var(--text); padding: 3px 4px; font-size: 12px; }
.msgs { flex: 1; overflow-y: auto; padding: 14px; display: flex; flex-direction: column; gap: 10px; }
.msg { max-width: 92%; padding: 9px 12px; border-radius: 12px; font-size: 14px; }
.msg.fabio { align-self: flex-end; background: var(--accent); color: #fff; border-bottom-right-radius: 4px; white-space: pre-wrap; }
.msg.oso { align-self: flex-start; background: var(--panel-2); border: 1px solid var(--line); border-bottom-left-radius: 4px; }
.msg .meta { font-size: 11px; opacity: .7; margin-top: 4px; }
.msg.oso .md p:first-child { margin-top: 0; } .msg.oso .md p:last-child { margin-bottom: 0; }
.typing { align-self: flex-start; color: var(--muted); font-size: 13px; display: flex; gap: 6px; align-items: center; }
.typing i { width: 6px; height: 6px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite; }
.typing i:nth-child(2) { animation-delay: .2s; } .typing i:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .2; } 40% { opacity: 1; } }
.composer { border-top: 1px solid var(--line); padding: 10px; display: flex; gap: 8px; align-items: flex-end; }
.composer textarea { flex: 1; resize: none; min-height: 42px; max-height: 180px; border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; background: var(--bg); color: var(--text); font: inherit; }
.composer button { background: var(--accent); color: #fff; border: 0; border-radius: 10px; padding: 10px 14px; cursor: pointer; font-weight: 600; }
.composer button:disabled { opacity: .5; cursor: default; }
.chat-hint { padding: 0 14px 10px; font-size: 11.5px; color: var(--muted); }
.chat-fab { display: none; }

/* viewer */
.crumbs { font-size: 13px; color: var(--muted); margin-bottom: 8px; }
.filelist a { display: block; padding: 6px 0; border-bottom: 1px solid var(--line); font-size: 13.5px; overflow-wrap: anywhere; }
pre.raw { white-space: pre-wrap; overflow-wrap: anywhere; font-family: var(--mono); font-size: 12.5px; background: var(--panel-2); padding: 12px; border-radius: 8px; max-height: 70vh; overflow: auto; }

/* responsive */
@media (max-width: 1200px) {
  .app { grid-template-columns: 200px minmax(0, 1fr) 340px; }
}
@media (max-width: 980px) {
  .app, .app.chat-closed { grid-template-columns: minmax(0, 1fr); }
  .side { position: fixed; inset: 0 auto 0 0; width: 250px; z-index: 30; transform: translateX(-100%); transition: transform .2s; box-shadow: 0 0 30px rgba(0,0,0,.2); }
  .side.open { transform: none; }
  .menu-btn { display: inline-block; }
  .chat { position: fixed; inset: 0; z-index: 40; display: none; border: 0; }
  .app .chat.open { display: flex; }
  .app.chat-closed .chat.open { display: flex; }
  .chat-toggle { display: none; }
  .chat-fab { display: flex; position: fixed; right: 16px; bottom: 16px; z-index: 20; background: var(--accent); color: #fff; border: 0; border-radius: 28px; padding: 12px 18px; font-weight: 600; box-shadow: 0 4px 16px rgba(0,0,0,.2); cursor: pointer; }
  .topbar { padding: 10px 16px; }
  .content { padding: 16px 16px 90px; }
  .search { width: 140px; }
  .two { grid-template-columns: minmax(0, 1fr); }
  .funnel { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .timeline .t { grid-template-columns: minmax(0, 1fr); gap: 2px; }
}
@media (max-width: 520px) {
  .search { display: none; }
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .kpi .value { font-size: 22px; }
}
.scrim { display: none; }
@media (max-width: 980px) { .scrim.on { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.3); z-index: 25; } }

/* login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login form { width: 100%; max-width: 340px; background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 28px; box-shadow: var(--shadow); }
.login h1 { margin: 0 0 4px; font-size: 22px; }
.login p { margin: 0 0 18px; color: var(--muted); font-size: 14px; }
.login input { width: 100%; padding: 11px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); color: var(--text); font: inherit; }
.login button { width: 100%; margin-top: 12px; padding: 11px; border-radius: 10px; border: 0; background: var(--accent); color: #fff; font-weight: 600; cursor: pointer; }
.login .err { color: var(--bad); font-size: 13px; min-height: 18px; margin-top: 8px; }
