:root {
  --wine: #6d1a2a; --wine-2: #8c2a3c; --wine-soft: #f5e9eb;
  --bg: #f7f4ef; --panel: #ffffff; --ink: #1f1a17; --muted: #6f6660; --line: #e6dfd6;
  --ok: #2f7a4a; --ok-soft: #e4f2e8; --warn: #9a6a00; --warn-soft: #fbf1d9; --err: #b3261e; --err-soft: #fbe6e4;
  --info: #28577e; --info-soft: #e3eef7;
  --radius: 12px; --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 16px rgba(0,0,0,.05);
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --serif: Georgia, "Iowan Old Style", "Times New Roman", serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #171412; --panel: #211d1a; --ink: #f1ebe4; --muted: #a79d94; --line: #36302b;
    --wine: #c4596b; --wine-2: #d46f80; --wine-soft: #3a2127;
    --ok: #71c28e; --ok-soft: #1f3226; --warn: #e2b755; --warn-soft: #3a3020; --err: #f08a80; --err-soft: #3d2220;
    --info: #86b6dc; --info-soft: #1e2d3a; color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--ink); font: 15px/1.5 var(--sans); }
body { min-height: 100dvh; -webkit-font-smoothing: antialiased; }
[hidden] { display: none !important; }
a { color: var(--wine); }
h1, h2, h3 { font-family: var(--serif); font-weight: 600; margin: 0 0 .4em; line-height: 1.2; }
h1 { font-size: 1.6rem; } h2 { font-size: 1.2rem; } h3 { font-size: 1.02rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.error { color: var(--err); min-height: 1.2em; }

/* ---------- shell ---------- */
.shell { display: grid; grid-template-columns: 232px 1fr; min-height: 100dvh; }
.sidebar { background: var(--panel); border-right: 1px solid var(--line); padding: 18px 12px; display: flex; flex-direction: column; gap: 2px; position: sticky; top: 0; height: 100dvh; }
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 10px 18px; }
.brand img { border-radius: 9px; }
.brand strong { font-family: var(--serif); font-size: 1.1rem; display: block; line-height: 1.1; }
.brand span { color: var(--muted); font-size: .8rem; letter-spacing: .08em; text-transform: uppercase; }
.sidebar a { display: flex; align-items: center; gap: 10px; padding: 9px 12px; border-radius: 9px; color: var(--ink); text-decoration: none; font-weight: 500; }
.sidebar a:hover { background: var(--bg); }
.sidebar a.active { background: var(--wine-soft); color: var(--wine); }
.sidebar .ico { width: 20px; text-align: center; opacity: .8; }
.count { margin-left: auto; background: var(--wine); color: #fff; border-radius: 99px; font-size: .72rem; padding: 1px 7px; font-weight: 600; }
.count:empty { display: none; }
.nav-foot { margin-top: auto; display: flex; flex-direction: column; gap: 6px; }
.main { padding: 28px 32px 60px; max-width: 1280px; width: 100%; min-width: 0; }

@media (max-width: 899px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar { position: fixed; bottom: 0; left: 0; right: 0; top: auto; height: auto; flex-direction: row; justify-content: space-around; padding: 6px 4px calc(6px + env(safe-area-inset-bottom)); border-right: 0; border-top: 1px solid var(--line); z-index: 20; }
  .brand, .desk { display: none !important; }
  .sidebar a { flex-direction: column; gap: 0; font-size: .7rem; padding: 6px 4px; flex: 1; position: relative; }
  .sidebar .ico { font-size: 1.15rem; }
  .count { position: absolute; top: 0; right: 18%; margin: 0; }
  .main { padding: calc(16px + env(safe-area-inset-top)) 16px 96px; }
}

/* ---------- componenti ---------- */
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 18px; }
.page-head p { margin: 0; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.btn { appearance: none; border: 1px solid var(--line); background: var(--panel); color: var(--ink); padding: 8px 14px; border-radius: 9px; font: inherit; font-weight: 500; cursor: pointer; display: inline-flex; align-items: center; gap: 6px; text-decoration: none; white-space: nowrap; }
.btn:hover { border-color: var(--muted); }
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--wine); border-color: var(--wine); color: #fff; }
.btn.primary:hover { background: var(--wine-2); }
.btn.ok { background: var(--ok); border-color: var(--ok); color: #fff; }
.btn.danger { color: var(--err); }
.btn.ghost { background: transparent; }
.btn.small { padding: 5px 10px; font-size: .85rem; }
.btn.icon { padding: 5px 8px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); }
.grid { display: grid; gap: 14px; }
.kpis { grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); margin-bottom: 18px; }
.kpi { text-decoration: none; color: inherit; display: block; }
.kpi .v { font-size: 1.9rem; font-family: var(--serif); font-weight: 600; line-height: 1.1; }
.kpi .l { color: var(--muted); font-size: .85rem; }
.kpi.accent .v { color: var(--wine); }
.two { grid-template-columns: 1fr 1fr; }
@media (max-width: 899px) { .two { grid-template-columns: 1fr; } }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 14px; overflow-x: auto; }
.tabs button { background: none; border: 0; padding: 10px 14px; font: inherit; font-weight: 600; color: var(--muted); border-bottom: 2px solid transparent; cursor: pointer; white-space: nowrap; }
.tabs button.active { color: var(--wine); border-color: var(--wine); }
.chips { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 12px; }
.chip { border: 1px solid var(--line); background: var(--panel); border-radius: 99px; padding: 5px 12px; font: inherit; font-size: .88rem; cursor: pointer; color: var(--ink); }
.chip.active { background: var(--ink); color: var(--bg); border-color: var(--ink); }
.chip b { font-weight: 600; opacity: .7; margin-left: 4px; }

.toolbar { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 12px; align-items: center; }
input, select, textarea { font: inherit; color: var(--ink); background: var(--panel); border: 1px solid var(--line); border-radius: 9px; padding: 8px 10px; width: 100%; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--wine-soft); border-color: var(--wine); }
input[type=checkbox], input[type=radio] { width: auto; accent-color: var(--wine); }
.toolbar input[type=search] { flex: 1; min-width: 180px; width: auto; }
.toolbar select { width: auto; }
label { display: block; font-weight: 500; font-size: .9rem; margin-bottom: 10px; }
label > input, label > select, label > textarea { margin-top: 4px; }
label.inline { display: flex; align-items: center; gap: 8px; font-weight: 400; }
textarea { resize: vertical; min-height: 90px; line-height: 1.5; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 0 14px; }
.hint { color: var(--muted); font-size: .82rem; font-weight: 400; margin-top: 3px; }

/* tabella responsive */
.table { width: 100%; border-collapse: collapse; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.table th, .table td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.table th { font-size: .78rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); font-weight: 600; background: var(--bg); }
.table tr:last-child td { border-bottom: 0; }
.table tbody tr:hover { background: color-mix(in srgb, var(--bg) 60%, transparent); }
.table td.act { white-space: nowrap; text-align: right; }
.table .name { font-weight: 600; }
.table .sub { color: var(--muted); font-size: .84rem; word-break: break-all; }
@media (max-width: 899px) {
  .table, .table tbody, .table tr, .table td { display: block; width: 100%; }
  .table thead { display: none; }
  .table { border: 0; background: none; }
  .table tr { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); margin-bottom: 10px; padding: 8px 4px; }
  .table td { border: 0; padding: 4px 12px; }
  .table td.act { text-align: left; }
  .table td[data-l]::before { content: attr(data-l) ": "; color: var(--muted); font-size: .8rem; }
}

.badge { display: inline-block; padding: 2px 8px; border-radius: 99px; font-size: .76rem; font-weight: 600; background: var(--bg); color: var(--muted); border: 1px solid var(--line); white-space: nowrap; }
.badge.ok { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.badge.warn { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.badge.err { background: var(--err-soft); color: var(--err); border-color: transparent; }
.badge.info { background: var(--info-soft); color: var(--info); border-color: transparent; }
.badge.wine { background: var(--wine-soft); color: var(--wine); border-color: transparent; }
.badge.dark { background: var(--ink); color: var(--bg); border-color: transparent; }
.dot { width: 9px; height: 9px; border-radius: 50%; display: inline-block; background: var(--line); }
.dot.on { background: var(--ok); } .dot.off { background: var(--err); }
.empty { text-align: center; padding: 48px 16px; color: var(--muted); }
.empty .big { font-size: 2rem; }

/* ---------- revisione bozze ---------- */
.review { display: grid; grid-template-columns: 340px 1fr; gap: 16px; align-items: start; }
.dlist { display: flex; flex-direction: column; gap: 6px; max-height: calc(100dvh - 220px); overflow-y: auto; padding-right: 2px; }
.ditem { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; cursor: pointer; display: grid; grid-template-columns: auto 1fr; gap: 2px 10px; text-align: left; font: inherit; color: inherit; }
.ditem:hover { border-color: var(--muted); }
.ditem.active { border-color: var(--wine); box-shadow: 0 0 0 1px var(--wine); }
.ditem input { grid-row: span 3; margin-top: 3px; }
.ditem .t { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ditem .s { color: var(--muted); font-size: .84rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.editor .meta { display: flex; flex-wrap: wrap; gap: 6px 14px; align-items: center; margin-bottom: 12px; color: var(--muted); font-size: .9rem; }
.editor textarea.body { min-height: 300px; font-family: var(--serif); font-size: 1rem; line-height: 1.6; }
.signature { white-space: pre-wrap; color: var(--muted); font-family: var(--serif); border-left: 3px solid var(--line); padding: 4px 12px; margin: 4px 0 14px; font-size: .92rem; }
.editor .bar { display: flex; gap: 8px; flex-wrap: wrap; justify-content: space-between; align-items: center; }
.qbar { display: flex; gap: 14px; flex-wrap: wrap; align-items: center; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 14px; margin-bottom: 14px; font-size: .9rem; }
@media (max-width: 899px) {
  .review { grid-template-columns: 1fr; }
  .dlist { max-height: none; }
  .review.editing .dlist, .review:not(.editing) .editor { display: none; }
}

/* ---------- inbox ---------- */
.conv { display: grid; grid-template-columns: 320px 1fr; gap: 16px; align-items: start; }
.thread { display: flex; flex-direction: column; gap: 10px; margin: 12px 0; }
.msg { max-width: 85%; padding: 10px 14px; border-radius: 12px; white-space: pre-wrap; word-wrap: break-word; border: 1px solid var(--line); }
.msg.in { background: var(--panel); align-self: flex-start; border-bottom-left-radius: 4px; }
.msg.out { background: var(--wine-soft); align-self: flex-end; border-bottom-right-radius: 4px; }
.msg .h { font-size: .78rem; color: var(--muted); margin-bottom: 4px; white-space: normal; }
.unread { font-weight: 700; }
.unread::before { content: "● "; color: var(--wine); }
@media (max-width: 899px) {
  .conv { grid-template-columns: 1fr; }
  .conv.open .dlist, .conv:not(.open) .convbody { display: none; }
}

/* ---------- login / modal / toast ---------- */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 16px; background: radial-gradient(circle at 30% 20%, var(--wine-soft), var(--bg) 60%); }
.login-card { background: var(--panel); padding: 32px 28px; border-radius: 16px; width: 100%; max-width: 360px; box-shadow: var(--shadow); text-align: center; border: 1px solid var(--line); }
.login-card img { border-radius: 14px; }
.login-card label { text-align: left; margin-top: 18px; }
.login-card .btn { width: 100%; justify-content: center; padding: 11px; }
.modal { position: fixed; inset: 0; background: rgba(20,12,10,.45); display: grid; place-items: center; z-index: 50; padding: 16px; }
.modal-card { background: var(--panel); border-radius: 16px; padding: 22px; width: 100%; max-width: 620px; max-height: 90dvh; overflow: auto; box-shadow: 0 20px 60px rgba(0,0,0,.25); }
.modal-card .bar { display: flex; justify-content: flex-end; gap: 8px; margin-top: 12px; }
.toasts { position: fixed; right: 16px; bottom: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 60; }
@media (max-width: 899px) { .toasts { left: 16px; bottom: 84px; } }
.toast { background: var(--ink); color: var(--bg); padding: 10px 14px; border-radius: 10px; box-shadow: var(--shadow); max-width: 420px; animation: in .2s ease-out; }
.toast.err { background: var(--err); color: #fff; }
@keyframes in { from { transform: translateY(8px); opacity: 0; } }
.offline { position: fixed; top: 0; left: 0; right: 0; background: var(--warn); color: #fff; text-align: center; padding: 6px; font-size: .85rem; z-index: 70; }
.spinner { width: 16px; height: 16px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; display: inline-block; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
details.section { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); margin-bottom: 12px; }
details.section > summary { padding: 14px 18px; cursor: pointer; font-weight: 600; font-family: var(--serif); font-size: 1.05rem; list-style: none; display: flex; justify-content: space-between; }
details.section > summary::after { content: "+"; color: var(--muted); }
details.section[open] > summary::after { content: "–"; }
details.section > div { padding: 0 18px 16px; }
.drop { border: 2px dashed var(--line); border-radius: var(--radius); padding: 36px 16px; text-align: center; background: var(--panel); cursor: pointer; }
.drop.over { border-color: var(--wine); background: var(--wine-soft); }
.scroll-x { overflow-x: auto; }
.table input, .table select { padding: 5px 7px; font-size: .88rem; min-width: 110px; }
.log { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: .8rem; max-height: 320px; overflow: auto; background: var(--bg); border-radius: 8px; padding: 8px 10px; }
.log div { padding: 2px 0; border-bottom: 1px dashed var(--line); }
.log .errore { color: var(--err); }
@media (min-width: 900px) { #back { display: none; } }
.table td.sub[data-l="Origine"] { white-space: nowrap; word-break: normal; }
.vars { display: flex; flex-wrap: wrap; gap: 5px; align-items: center; margin: 6px 0 2px; font-weight: 400; font-size: .82rem; color: var(--muted); }
.chip.small { padding: 2px 9px; font-size: .8rem; }
.table th.selc, .table td.selc { width: 34px; padding-right: 0; }
@media (max-width: 899px) { .table td.selc { float: right; width: auto; } }
