:root {
  --ink: #14304A;        /* blu carta intestata */
  --ink-2: #2B5079;
  --paper: #FAFBFC;
  --line: #DCE3EA;
  --mist: #EEF2F6;
  --text: #1B2430;
  --muted: #5B6878;
  --alarm: #C8102E;
  --alarm-dark: #9E0B23;
  --ok: #1F7A4D;
  --ok-bg: #E4F2EA;
  --wait: #8A5A00;
  --wait-bg: #FFF2D6;
  --radius-s: 6px;
  --radius-m: 10px;
  --shadow: 0 1px 2px rgba(20, 48, 74, .08), 0 4px 16px rgba(20, 48, 74, .06);
  font-family: 'IBM Plex Sans', system-ui, sans-serif;
  color: var(--text);
  background: var(--paper);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font-size: 15px; line-height: 1.5; -webkit-font-smoothing: antialiased; }
button, input, textarea, select { font: inherit; color: inherit; }
a { color: var(--ink-2); }
:focus-visible { outline: 3px solid #6CA0D6; outline-offset: 2px; }
[hidden] { display: none !important; }

/* ---------- pulsanti e campi */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--line); background: #fff; border-radius: var(--radius-s);
  padding: 9px 16px; font-weight: 500; cursor: pointer; white-space: nowrap;
}
.btn:hover { background: var(--mist); }
a.btn { text-decoration: none; }
.btn svg { width: 18px; height: 18px; fill: currentColor; }
.btn-primary { background: var(--ink); color: #fff; border-color: var(--ink); }
.btn-primary:hover { background: var(--ink-2); }
.btn-danger { color: var(--alarm); border-color: #F0C2CA; }
.btn-ok { background: var(--ok); color: #fff; border-color: var(--ok); }
.btn-ok:hover { background: #18633E; }
.btn-small { padding: 5px 10px; font-size: 13px; }
.btn-white { background: #fff; color: var(--alarm); border-color: #fff; font-weight: 600; }
.btn-on-red { background: transparent; color: #fff; border-color: rgba(255,255,255,.6); }
.btn-on-red:hover { background: rgba(255,255,255,.12); }
.btn[disabled] { opacity: .55; cursor: default; }
.icon-btn { border: 0; background: transparent; padding: 8px; border-radius: var(--radius-s); cursor: pointer; color: var(--muted); }
.icon-btn:hover { background: var(--mist); color: var(--text); }
.icon-btn svg { width: 20px; height: 20px; display: block; fill: currentColor; }

label.field { display: block; margin-bottom: 14px; }
label.field > span { display: block; font-size: 13px; font-weight: 500; color: var(--muted); margin-bottom: 4px; }
input[type=text], input[type=password], textarea, select {
  width: 100%; border: 1px solid var(--line); border-radius: var(--radius-s); background: #fff;
  padding: 9px 11px;
}
textarea { resize: vertical; min-height: 90px; }
input:focus, textarea:focus, select:focus { outline: 2px solid #6CA0D6; outline-offset: 0; border-color: #6CA0D6; }
.hint { font-size: 13px; color: var(--muted); margin: 4px 0 0; }
.error { color: var(--alarm); font-size: 14px; min-height: 1.4em; margin: 6px 0; }

/* ---------- accesso */
.auth { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; background: var(--ink); }
.auth-card { width: 100%; max-width: 380px; background: #fff; border-radius: var(--radius-m); padding: 32px 28px; box-shadow: var(--shadow); }
.auth-brand { display: flex; align-items: center; gap: 12px; margin-bottom: 24px; }
.auth-brand img { width: 44px; height: 44px; border-radius: 8px; }
.auth-brand b { display: block; font-size: 18px; color: var(--ink); line-height: 1.2; }
.auth-brand small { color: var(--muted); }
.auth-card h1 { font-size: 20px; margin: 0 0 16px; }
.auth-card .btn { width: 100%; padding: 11px; }

/* ---------- struttura principale */
.shell { display: grid; grid-template-columns: 340px 1fr; height: 100vh; height: 100dvh; }
.sidebar { border-right: 1px solid var(--line); background: #fff; display: flex; flex-direction: column; min-height: 0; }
.side-head { display: flex; align-items: center; gap: 10px; padding: 14px 14px 12px 16px; border-bottom: 1px solid var(--line); }
.side-head .who { flex: 1; min-width: 0; }
.side-head .who b { display: block; color: var(--ink); font-size: 15px; line-height: 1.2; }
.side-head .who small { color: var(--muted); font-size: 12px; }
.side-head img { width: 34px; height: 34px; border-radius: 7px; }
.side-new { padding: 12px 14px; }
.side-new .btn { width: 100%; }
.thread-list { list-style: none; margin: 0; padding: 0 0 20px; overflow-y: auto; flex: 1; }
.thread-item a {
  display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; padding: 11px 16px;
  text-decoration: none; color: inherit; border-left: 3px solid transparent;
}
.thread-item a:hover { background: var(--mist); }
.thread-item.active a { background: var(--mist); border-left-color: var(--ink); }
.thread-item.ringing a { border-left-color: var(--alarm); background: #FDF1F3; }
.t-subject { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.thread-item.read .t-subject { font-weight: 500; }
.t-time { font-size: 12px; color: var(--muted); }
.t-snippet { grid-column: 1 / -1; font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.t-people { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.t-flags { display: flex; gap: 4px; justify-content: flex-end; }
.pill { font-size: 11px; font-weight: 600; padding: 1px 7px; border-radius: 99px; white-space: nowrap; }
.pill-alarm { background: var(--alarm); color: #fff; }
.pill-wait { background: var(--wait-bg); color: var(--wait); }
.pill-new { background: var(--ink); color: #fff; }
.empty-list { padding: 24px 16px; color: var(--muted); font-size: 14px; }

.content { min-width: 0; min-height: 0; display: flex; flex-direction: column; }
.content-head { display: flex; align-items: center; gap: 10px; padding: 12px 20px; border-bottom: 1px solid var(--line); background: #fff; min-height: 59px; }
.content-head h2 { font-size: 17px; margin: 0; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.content-head .sub { display: block; font-size: 12px; font-weight: 400; color: var(--muted); }
.back { display: none; }
.content-body { flex: 1; overflow-y: auto; padding: 20px; }
.placeholder { height: 100%; display: grid; place-items: center; text-align: center; color: var(--muted); }
.placeholder p { max-width: 320px; }

/* ---------- messaggi */
.msg { max-width: 760px; margin: 0 0 18px; }
.msg-card { background: #fff; border: 1px solid var(--line); border-radius: var(--radius-m); padding: 14px 16px; }
.msg.mine .msg-card { background: #F2F6FA; }
.msg-head { display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 10px; margin-bottom: 6px; }
.msg-head b { color: var(--ink); }
.msg-head time { font-size: 12px; color: var(--muted); }
.msg-body { white-space: pre-wrap; word-wrap: break-word; }
.files { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.file {
  display: flex; align-items: center; gap: 10px; border: 1px solid var(--line); border-radius: var(--radius-s);
  padding: 7px 10px; text-decoration: none; color: inherit; background: #fff; max-width: 100%;
}
.file:hover { border-color: var(--ink-2); }
.file .ext { font-size: 11px; font-weight: 700; color: #fff; background: var(--ink-2); border-radius: 4px; padding: 3px 5px; min-width: 38px; text-align: center; }
.file .ext.pdf { background: #B3261E; } .file .ext.doc { background: #1F5BB4; } .file .ext.xls { background: #1F7A4D; } .file .ext.img { background: #6A4BA0; }
.file .fn { font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 240px; }
.file .fs { font-size: 12px; color: var(--muted); }
.thumb { display: block; max-width: 220px; max-height: 160px; border-radius: var(--radius-s); border: 1px solid var(--line); object-fit: cover; }

.status { margin-top: 10px; padding-top: 10px; border-top: 1px dashed var(--line); display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.status-label { font-size: 12px; color: var(--muted); margin-right: 2px; }
.chip { display: inline-flex; align-items: center; gap: 5px; font-size: 12px; padding: 2px 9px; border-radius: 99px; background: var(--mist); }
.chip::before { content: ''; width: 7px; height: 7px; border-radius: 50%; background: var(--muted); }
.chip.ack { background: var(--ok-bg); color: var(--ok); }
.chip.ack::before { background: var(--ok); }
.chip.ring { background: #FDE7EA; color: var(--alarm-dark); }
.chip.ring::before { background: var(--alarm); animation: blink 1s steps(2) infinite; }
.chip.seen { background: var(--wait-bg); color: var(--wait); }
.chip.seen::before { background: var(--wait); }
.status .btn { margin-left: auto; }
@keyframes blink { 50% { opacity: .2; } }

.inbox-alert { max-width: 760px; display: flex; align-items: center; gap: 12px; background: #FDE7EA; border: 1px solid #F3B6C0; color: var(--alarm-dark); border-radius: var(--radius-m); padding: 12px 14px; margin-bottom: 18px; }
.inbox-alert span { flex: 1; }

/* ---------- risposta */
.reply { border-top: 1px solid var(--line); background: #fff; padding: 12px 20px; }
.reply-row { display: flex; gap: 8px; align-items: flex-end; max-width: 760px; }
.reply textarea { min-height: 44px; height: 44px; max-height: 200px; }
.reply-opts { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 14px; margin-top: 6px; font-size: 13px; color: var(--muted); max-width: 760px; }
.check { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; }
.check input { width: 16px; height: 16px; accent-color: var(--ink); }
.picked { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.picked .chip::before { display: none; }
.picked button { border: 0; background: none; padding: 0 0 0 2px; cursor: pointer; color: var(--muted); }

/* ---------- nuovo messaggio */
.compose { max-width: 760px; }
.people { border: 1px solid var(--line); border-radius: var(--radius-s); background: #fff; max-height: 260px; overflow-y: auto; }
.people-tools { display: flex; gap: 8px; margin-bottom: 8px; }
.people-tools input { flex: 1; }
.person { display: flex; align-items: center; gap: 10px; padding: 8px 12px; border-bottom: 1px solid var(--mist); cursor: pointer; }
.person:last-child { border-bottom: 0; }
.person:hover { background: var(--mist); }
.person input { width: 17px; height: 17px; accent-color: var(--ink); }
.person small { color: var(--muted); }
.drop { border: 2px dashed var(--line); border-radius: var(--radius-m); padding: 18px; text-align: center; color: var(--muted); cursor: pointer; background: #fff; }
.drop.over { border-color: var(--ink-2); background: #F2F6FA; }
.drop strong { color: var(--ink-2); }
.file-queue { list-style: none; padding: 0; margin: 8px 0 0; }
.file-queue li { display: flex; align-items: center; gap: 8px; font-size: 14px; padding: 4px 0; }
.file-queue li span:first-child { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.progress { height: 4px; background: var(--mist); border-radius: 2px; overflow: hidden; margin: 10px 0; }
.progress div { height: 100%; background: var(--ink); width: 0; transition: width .2s; }
.repeat-box { display: flex; gap: 10px; align-items: flex-start; border: 1px solid var(--line); background: #fff; border-radius: var(--radius-m); padding: 12px 14px; margin: 16px 0; }
.repeat-box input { width: 18px; height: 18px; margin-top: 2px; accent-color: var(--alarm); }
.repeat-box b { display: block; }
.repeat-box small { color: var(--muted); }

/* ---------- impostazioni */
.settings { max-width: 760px; }
.settings section { background: #fff; border: 1px solid var(--line); border-radius: var(--radius-m); padding: 18px 20px; margin-bottom: 18px; }
.settings h3 { margin: 0 0 4px; font-size: 16px; color: var(--ink); }
.settings h3 + p { margin: 0 0 14px; color: var(--muted); font-size: 14px; }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: flex-end; }
.row > label.field { flex: 1; min-width: 180px; margin-bottom: 0; }
.status-line { display: flex; align-items: center; gap: 8px; font-size: 14px; margin: 10px 0 0; }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--muted); }
.dot.ok { background: var(--ok); } .dot.bad { background: var(--alarm); }
table.users { width: 100%; border-collapse: collapse; font-size: 14px; }
table.users th { text-align: left; font-weight: 500; color: var(--muted); font-size: 13px; padding: 6px 8px; border-bottom: 1px solid var(--line); }
table.users td { padding: 8px; border-bottom: 1px solid var(--mist); vertical-align: middle; }
table.users tr.off td { color: var(--muted); }
table.users td:last-child { text-align: right; white-space: nowrap; }
.role-admin { font-size: 11px; font-weight: 600; color: var(--ink); background: var(--mist); padding: 1px 7px; border-radius: 99px; margin-left: 6px; }
details.add-user { margin-top: 14px; }
details.add-user summary { cursor: pointer; color: var(--ink-2); font-weight: 500; }
details.add-user .grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; margin-top: 12px; }

/* ---------- allarme */
.alarm {
  position: fixed; inset: 0 0 auto 0; z-index: 50; display: flex; align-items: center; gap: 12px;
  background: var(--alarm); color: #fff; padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top));
  box-shadow: 0 4px 20px rgba(158, 11, 35, .35);
}
.alarm-bell { width: 26px; height: 26px; fill: #fff; flex: none; transform-origin: 50% 10%; animation: ring 2s ease-in-out infinite; }
.alarm-text { flex: 1; min-width: 0; }
.alarm-text strong { display: block; line-height: 1.25; }
.alarm-text span { font-size: 13px; opacity: .9; display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
body.has-alarm .shell, body.has-alarm .auth { padding-top: var(--alarm-h, 58px); }
@keyframes ring {
  0%, 50%, 100% { transform: rotate(0); }
  5%, 15%, 25% { transform: rotate(14deg); }
  10%, 20%, 30% { transform: rotate(-14deg); }
}
.sound-unlock { position: fixed; z-index: 49; left: 50%; transform: translateX(-50%); bottom: 16px; background: var(--ink); color: #fff; border-radius: var(--radius-m); padding: 10px 12px 10px 16px; display: flex; gap: 12px; align-items: center; font-size: 14px; box-shadow: var(--shadow); max-width: calc(100% - 32px); }
.toast { position: fixed; z-index: 60; left: 50%; bottom: 20px; transform: translateX(-50%); background: var(--text); color: #fff; padding: 10px 16px; border-radius: var(--radius-s); font-size: 14px; max-width: calc(100% - 32px); }

@media (prefers-reduced-motion: reduce) {
  .alarm-bell, .chip.ring::before { animation: none; }
}

/* ---------- cellulare */
@media (max-width: 760px) {
  .shell { grid-template-columns: 1fr; }
  .shell.detail .sidebar { display: none; }
  .shell:not(.detail) .content { display: none; }
  .back { display: inline-flex; }
  .content-head { padding: 10px 12px; padding-top: max(10px, env(safe-area-inset-top)); }
  .side-head { padding-top: max(14px, env(safe-area-inset-top)); }
  .content-body { padding: 14px 12px; }
  .reply { padding: 10px 12px; padding-bottom: max(10px, env(safe-area-inset-bottom)); }
  details.add-user .grid { grid-template-columns: 1fr; }
  table.users th:nth-child(2), table.users td:nth-child(2) { display: none; }
  .alarm { flex-wrap: wrap; }
  .alarm-text { flex-basis: calc(100% - 40px); }
  .alarm .btn { flex: 1; }
  .file .fn { max-width: 160px; }
}
