:root {
  --bg: #f5f4f0;
  --surface: #ffffff;
  --ink: #1c1b19;
  --muted: #6a665e;
  --line: #e4e1d9;
  --accent: #1f3d36;
  --accent-soft: #e3ece8;
  --danger: #a3352b;
  --radius: 12px;
  --c-ordini: #2f6f5e;
  --c-pagamenti: #8a6a1f;
  --c-spedizioni: #3c5d8f;
  --c-report: #6b4f86;
  --c-whatsapp: #2e8b57;
  --c-email: #8b4a3c;
  --c-instagram: #a4416f;
  --c-sistema: #a3352b;
  --c-altro: #6a665e;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
html { background: #f5f4f0; }
body {
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
  padding-bottom: env(safe-area-inset-bottom);
}
button, input, select { font: inherit; color: inherit; }
a { color: var(--accent); }
.boot { padding: 40vh 0 0; text-align: center; color: var(--muted); }

/* ---- accesso ---- */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 24px; }
.auth-card { width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--line); border-radius: 16px; padding: 28px 24px; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 650; font-size: 18px; margin-bottom: 20px; }
.brand img { width: 32px; height: 32px; border-radius: 8px; }
.auth h1 { font-size: 20px; margin: 0 0 6px; }
.auth p { color: var(--muted); margin: 0 0 18px; font-size: 15px; }
.qr { display: block; margin: 8px auto 12px; width: 200px; height: 200px; }
.secret { font-family: ui-monospace, Menlo, monospace; font-size: 13px; background: var(--bg); padding: 8px 10px; border-radius: 8px; word-break: break-all; text-align: center; margin-bottom: 16px; }

/* ---- form ---- */
label { display: block; font-size: 14px; font-weight: 550; margin: 0 0 6px; }
.field { margin-bottom: 14px; }
input[type=text], input[type=email], input[type=password], input[inputmode=numeric], select {
  width: 100%; padding: 11px 12px; border: 1px solid var(--line); border-radius: 10px; background: #fff;
}
input:focus, select:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.code-input { letter-spacing: 6px; text-align: center; font-size: 22px; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 10px 16px; border-radius: 10px; border: 1px solid var(--line); background: #fff; cursor: pointer;
  font-weight: 550; font-size: 15px; text-decoration: none; color: var(--ink);
}
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-danger { color: var(--danger); }
.btn-block { width: 100%; }
.btn:disabled { opacity: .55; cursor: default; }
.btn-small { padding: 6px 10px; font-size: 14px; }
.error { color: var(--danger); font-size: 14px; min-height: 20px; margin: 4px 0 10px; }
.hint { color: var(--muted); font-size: 14px; }

/* ---- struttura ---- */
.topbar {
  position: sticky; top: 0; z-index: 5; background: rgba(245,244,240,.92); backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line); padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
}
.topbar-inner { max-width: 760px; margin: 0 auto; display: flex; align-items: center; gap: 12px; }
.topbar .brand { margin: 0; font-size: 17px; flex: 1; }
.badge { background: var(--accent); color: #fff; border-radius: 999px; font-size: 12px; padding: 1px 8px; font-weight: 600; }
.nav { display: flex; gap: 4px; }
.nav a { padding: 7px 10px; border-radius: 8px; text-decoration: none; color: var(--muted); font-size: 14px; font-weight: 550; }
.nav a.active { background: var(--accent-soft); color: var(--accent); }
main { max-width: 760px; margin: 0 auto; padding: 16px; }
h2 { font-size: 18px; margin: 4px 0 12px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin-bottom: 14px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }

/* ---- feed ---- */
.chips { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 4px; margin-bottom: 10px; scrollbar-width: none; }
.chip { white-space: nowrap; border: 1px solid var(--line); background: #fff; border-radius: 999px; padding: 6px 12px; font-size: 14px; cursor: pointer; }
.chip.active { background: var(--ink); color: #fff; border-color: var(--ink); }
.feed { list-style: none; padding: 0; margin: 0; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.feed li + li { border-top: 1px solid var(--line); }
.ev { display: grid; grid-template-columns: 10px 1fr auto; gap: 12px; padding: 13px 14px; text-decoration: none; color: inherit; }
.ev:hover { background: #faf9f6; }
.dot { width: 10px; height: 10px; border-radius: 50%; margin-top: 7px; background: var(--c-altro); }
.ev.read .dot { opacity: .25; }
.ev-title { font-weight: 600; }
.ev.read .ev-title { font-weight: 500; }
.ev-body { color: var(--muted); font-size: 14px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.ev-meta { color: var(--muted); font-size: 12px; text-align: right; white-space: nowrap; }
.ev-chan { font-size: 12px; color: var(--muted); }
.empty { padding: 36px 16px; text-align: center; color: var(--muted); }
.detail-body { white-space: pre-wrap; margin: 10px 0 0; }
.kv { display: grid; grid-template-columns: 130px 1fr; gap: 6px 12px; font-size: 14px; margin-top: 14px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; word-break: break-word; }

.ch-ordini { background: var(--c-ordini); } .ch-pagamenti { background: var(--c-pagamenti); }
.ch-spedizioni { background: var(--c-spedizioni); } .ch-report { background: var(--c-report); }
.ch-whatsapp { background: var(--c-whatsapp); } .ch-email { background: var(--c-email); }
.ch-instagram { background: var(--c-instagram); } .ch-sistema { background: var(--c-sistema); }

/* ---- impostazioni / utenti ---- */
.status { display: inline-flex; align-items: center; gap: 6px; font-size: 14px; font-weight: 550; }
.status::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.status.on::before { background: #2e8b57; }
.status.off::before { background: var(--danger); }
.checks { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 8px; }
.check { display: flex; gap: 8px; align-items: center; font-weight: 450; font-size: 15px; margin: 0; }
.notice { background: #fbf6e6; border: 1px solid #ecdfb4; border-radius: 10px; padding: 10px 12px; font-size: 14px; margin: 10px 0; }
.user-row { display: flex; gap: 10px; align-items: center; padding: 12px 0; border-top: 1px solid var(--line); }
.user-row:first-child { border-top: 0; }
.user-name { font-weight: 600; }
.role { font-size: 12px; border-radius: 6px; padding: 2px 7px; background: var(--bg); color: var(--muted); font-weight: 600; }
.perm-ch { border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; margin-bottom: 8px; }
.perm-types { display: flex; flex-wrap: wrap; gap: 6px 14px; margin: 8px 0 0 26px; }
.perm-types .check { font-size: 14px; }
.temp-pwd { font-family: ui-monospace, Menlo, monospace; font-size: 18px; background: var(--accent-soft); padding: 10px; border-radius: 8px; text-align: center; user-select: all; }
table.log { width: 100%; border-collapse: collapse; font-size: 13px; }
table.log td, table.log th { text-align: left; padding: 6px 4px; border-top: 1px solid var(--line); }

#toast {
  position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--ink); color: #fff; padding: 10px 16px; border-radius: 10px; font-size: 14px; z-index: 50; max-width: 90vw;
}
@media (max-width: 520px) {
  .nav a { padding: 6px 8px; font-size: 13px; }
  .topbar .brand span.name { display: none; }
  .kv { grid-template-columns: 100px 1fr; }
}

.sep { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 13px; margin: 8px 0 16px; }
.sep::before, .sep::after { content: ""; flex: 1; height: 1px; background: var(--line); }

/* ---- chat ---- */
.main-wide { max-width: 1180px; padding: 12px 16px; display: flex; flex-direction: column;
  height: calc(100vh - 57px - env(safe-area-inset-top)); height: calc(100dvh - 57px - env(safe-area-inset-top)); }
.main-wide > .notice { flex: none; }
.chat-layout { flex: 1; min-height: 0; display: grid; grid-template-columns: minmax(0, 1fr); grid-template-rows: minmax(0, 1fr); background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.chat-list-head .chip { padding: 5px 10px; font-size: 13px; }
.chat-layout .chat-view { display: none; }
.chat-layout.has-conv .chat-list { display: none; }
.chat-layout.has-conv .chat-view { display: flex; }
.chat-list { display: flex; flex-direction: column; min-height: 0; min-width: 0; border-right: 1px solid var(--line); }
.chat-list-head { padding: 12px 12px 8px; border-bottom: 1px solid var(--line); }
.chat-list-head .chips { margin-bottom: 8px; }
.chat-search { width: 100%; padding: 9px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); }
.chip-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; vertical-align: 1px; }
.chat-items { overflow-y: auto; flex: 1; }
.chat-item { display: flex; gap: 12px; align-items: center; padding: 11px 14px; text-decoration: none; color: inherit; border-bottom: 1px solid #f0eee9; }
.chat-item:hover { background: #faf9f6; }
.chat-item.selected { background: var(--accent-soft); }
.avatar { position: relative; flex: none; width: 44px; height: 44px; border-radius: 50%; display: grid; place-items: center; color: #fff; font-weight: 650; font-size: 15px; background: var(--c-altro); }
.ch-bg-whatsapp { background: #3f8f63; } .ch-bg-instagram { background: #a4416f; } .ch-bg-facebook { background: #3b5f9e; }
.avatar-ch { position: absolute; right: -2px; bottom: -2px; width: 18px; height: 18px; border-radius: 50%; border: 2px solid #fff; font-size: 9px; display: grid; place-items: center; color: #fff; }
.ch-facebook { background: #3b5f9e; }
.chat-main { flex: 1; min-width: 0; }
.chat-top, .chat-bottom { display: flex; align-items: center; gap: 8px; }
.chat-name { font-weight: 600; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-time { font-size: 12px; color: var(--muted); }
.chat-item.unread .chat-time { color: var(--accent); font-weight: 600; }
.chat-prev { flex: 1; min-width: 0; font-size: 14px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-item.unread .chat-prev { color: var(--ink); }
.you { color: var(--accent); font-weight: 600; }
.chat-view { flex-direction: column; min-height: 0; background: #efeae2; }
.conv-head { display: flex; align-items: center; gap: 12px; padding: 10px 14px; background: var(--surface); border-bottom: 1px solid var(--line); }
.conv-head .avatar { width: 38px; height: 38px; font-size: 14px; }
.conv-back { text-decoration: none; font-size: 22px; color: var(--ink); padding: 0 4px; }
.conv-title { min-width: 0; flex: 1; }
.conv-body { flex: 1; overflow-y: auto; padding: 12px 4% 16px; }
.conv-foot { padding: 10px 14px; text-align: center; font-size: 13px; color: var(--muted); background: var(--surface); border-top: 1px solid var(--line); }
.conv-empty { margin: auto; color: var(--muted); }
.day-sep { text-align: center; margin: 12px 0 8px; }
.day-sep span { background: #fff; border-radius: 8px; padding: 4px 10px; font-size: 12px; color: var(--muted); box-shadow: 0 1px 1px rgba(0,0,0,.06); text-transform: capitalize; }
.msg { display: flex; margin: 3px 0; }
.msg.out { justify-content: flex-end; }
.bubble { max-width: min(78%, 560px); background: #fff; border-radius: 10px; padding: 6px 9px 4px; box-shadow: 0 1px 1px rgba(0,0,0,.08); }
.msg.out .bubble { background: #dbf5d6; }
.msg-sender { font-size: 12px; font-weight: 650; color: var(--accent); margin-bottom: 2px; }
.msg-type { font-size: 12px; color: var(--muted); font-style: italic; }
.msg-text { white-space: pre-wrap; word-break: break-word; font-size: 15px; }
.msg-time { text-align: right; font-size: 11px; color: var(--muted); margin-top: 1px; }
@media (min-width: 860px) {
  .chat-layout { grid-template-columns: 360px minmax(0, 1fr); }
  .chat-layout .chat-list, .chat-layout.has-conv .chat-list { display: flex; }
  .chat-layout .chat-view, .chat-layout.has-conv .chat-view { display: flex; }
  .conv-back { display: none; }
}
@media (max-width: 859px) {
  .main-wide { padding: 0; }
  .main-wide .notice { margin: 8px; }
  .chat-layout { border: 0; border-radius: 0; }
}
