:root {
  --bg: #f4f5f7;
  --surface: #fff;
  --border: #d9dde3;
  --text: #1c2430;
  --muted: #6b7686;
  --primary: #128c7e;
  --primary-text: #fff;
  --danger: #c62828;
  --warn-bg: #fff4d6;
  --warn-text: #7a5600;
  --s-connected: #1b8a3d;
  --s-pairing: #d98a00;
  --s-disconnected: #c62828;
  --s-unpaired: #6b7686;
  --s-logged_out: #6b7686;
  --radius: 8px;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 15px;
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); }
[hidden] { display: none !important; }
h1, h2, h3 { margin: 0 0 .5rem; }
h1 { font-size: 1.3rem; }
h2 { font-size: 1.25rem; }
h3 { font-size: 1rem; }
code, pre { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: .85em; }
.muted { color: var(--muted); }
.small { font-size: .85em; }

button {
  font: inherit; padding: .45rem .9rem; border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface); color: var(--text); cursor: pointer;
}
button:hover { border-color: var(--muted); }
button.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
button.danger { color: var(--danger); border-color: var(--danger); }
button.link { border: 0; background: none; color: var(--muted); text-decoration: underline; padding: 0; }
button:disabled { opacity: .5; cursor: default; }
input, textarea {
  width: 100%; font: inherit; padding: .45rem .6rem; border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface); color: var(--text);
}
label { display: block; margin-bottom: .8rem; font-weight: 600; font-size: .9rem; }
label > input, label > textarea, label > .row { margin-top: .25rem; font-weight: 400; }
.row { display: flex; gap: .5rem; align-items: center; }
.row.end { justify-content: flex-end; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem; margin-bottom: 1rem; }

.login { min-height: 100vh; display: grid; place-items: center; }
.login-card { width: min(380px, 92vw); }

.app { display: grid; grid-template-columns: 300px 1fr; min-height: 100vh; }
.sidebar { background: var(--surface); border-right: 1px solid var(--border); display: flex; flex-direction: column; }
.sidebar-head { display: flex; justify-content: space-between; align-items: center; padding: 1rem; border-bottom: 1px solid var(--border); }
.sidebar-head h1 { margin: 0; }
.sidebar-foot { padding: .8rem 1rem; border-top: 1px solid var(--border); }
.channel-list { list-style: none; margin: 0; padding: 0; flex: 1; overflow-y: auto; }
.channel-list li { padding: .7rem 1rem; border-bottom: 1px solid var(--border); cursor: pointer; }
.channel-list li:hover { background: var(--bg); }
.channel-list li.active { background: #e3f3f0; border-left: 3px solid var(--primary); }
.channel-list .name { font-weight: 600; display: flex; justify-content: space-between; gap: .5rem; }
.main { padding: 1.25rem 1.5rem; min-width: 0; }
.empty { padding: 3rem 0; text-align: center; }

.detail-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 1rem; margin-bottom: 1rem; }
.head-actions { display: flex; align-items: center; gap: .6rem; }
.tabs { display: flex; gap: .25rem; border-bottom: 1px solid var(--border); margin-bottom: 1rem; }
.tabs button { border: 0; border-bottom: 3px solid transparent; border-radius: 0; background: none; }
.tabs button.active { border-bottom-color: var(--primary); font-weight: 600; }

.badge {
  display: inline-block; padding: .1rem .55rem; border-radius: 999px; font-size: .75rem; font-weight: 600;
  background: var(--muted); color: #fff; white-space: nowrap;
}
.badge.s-connected { background: var(--s-connected); }
.badge.s-pairing { background: var(--s-pairing); }
.badge.s-disconnected { background: var(--s-disconnected); }
.badge.s-unpaired, .badge.s-logged_out { background: var(--s-unpaired); }
.badge.s-halted { background: var(--warn-text); }
.badge.s-failed { background: var(--s-disconnected); }
.badge.s-read, .badge.s-delivered { background: var(--s-connected); }
.badge.s-sent { background: #2f6fd1; }
.badge.s-queued { background: var(--s-pairing); }

.qr-box { display: grid; place-items: center; width: 300px; height: 300px; border: 1px dashed var(--border); border-radius: var(--radius); margin-bottom: .5rem; }
.qr-box img { width: 300px; height: 300px; }
.pair-code { font-size: 2.2rem; letter-spacing: .25em; font-weight: 700; font-family: ui-monospace, Menlo, monospace; margin-top: 1rem; user-select: all; }
.json { background: var(--bg); border-radius: var(--radius); padding: .7rem; margin: 0; overflow: auto; }

.table { width: 100%; border-collapse: collapse; background: var(--surface); border: 1px solid var(--border); }
.table th, .table td { text-align: left; padding: .45rem .7rem; border-bottom: 1px solid var(--border); }
.yes { color: var(--s-connected); font-weight: 600; }
.no { color: var(--danger); font-weight: 600; }

.events { list-style: none; margin: 0; padding: 0; }
.events li { display: flex; gap: .7rem; align-items: baseline; padding: .4rem .6rem; border-bottom: 1px solid var(--border); background: var(--surface); }
.events .time { color: var(--muted); font-size: .8rem; white-space: nowrap; }
.events .payload { overflow-wrap: anywhere; font-family: ui-monospace, Menlo, monospace; font-size: .8rem; }
.events .badge { background: #3a4a63; flex: none; }
.events .badge.ev-error { background: var(--danger); }

.modal { position: fixed; inset: 0; background: rgba(0, 0, 0, .45); display: grid; place-items: center; z-index: 10; }
.modal-box { width: min(480px, 92vw); margin: 0; }
.warn { background: var(--warn-bg); color: var(--warn-text); padding: .6rem .8rem; border-radius: var(--radius); }

.toasts { position: fixed; right: 1rem; bottom: 1rem; display: flex; flex-direction: column; gap: .5rem; z-index: 20; }
.toast { background: var(--text); color: #fff; padding: .6rem .9rem; border-radius: var(--radius); max-width: 360px; }
.toast.error { background: var(--danger); }

@media (max-width: 1100px) { .app { grid-template-columns: 250px 1fr; } .main { padding: 1rem; } }

.subtabs { display: flex; gap: .25rem; margin-bottom: .75rem; }
.subtabs button { border-radius: 999px; font-size: .85rem; }
.subtabs button.active { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: .6rem; }
.queue-box .row { justify-content: space-between; }
.countdown { font-size: 2rem; font-weight: 700; font-family: ui-monospace, Menlo, monospace; margin: .25rem 0; }
.sent-text { white-space: pre-wrap; }

.tpl-details { margin: .25rem 0 .9rem; }
.tpl-details summary { cursor: pointer; font-weight: 600; font-size: .9rem; padding: .35rem 0; }
.tpl-rows { list-style: none; margin: .5rem 0; padding: 0; }
.tpl-rows li { display: flex; gap: .5rem; align-items: flex-start; margin-bottom: .5rem; }
.tpl-rows li .num { color: var(--muted); font-size: .8rem; width: 1.5rem; padding-top: .55rem; text-align: right; }
.tpl-rows li textarea { flex: 1; min-height: 2.4rem; resize: vertical; }
.tpl-rows li button { flex: none; margin-top: .15rem; }

/* Modalità africana */
.sidebar-foot { display: flex; justify-content: flex-end; align-items: center; gap: .5rem; }
.sidebar-mode { padding: .6rem 1rem; border-bottom: 1px solid var(--border); background: var(--bg); }
.switch { display: inline-flex; align-items: center; gap: .5rem; font-size: .85rem; font-weight: 500; cursor: pointer; margin: 0; }
.switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.switch .slider { width: 34px; height: 20px; border-radius: 999px; background: var(--border); position: relative; transition: background .25s ease-in-out; flex: none; }
.switch .slider::after { content: ''; position: absolute; top: 3px; left: 3px; width: 14px; height: 14px; border-radius: 50%; background: #fff; transition: transform .25s ease-in-out; }
.switch input:checked + .slider { background: #d4860b; }
.switch input:checked + .slider::after { transform: translateX(14px); }
#african-layer { position: fixed; inset: 0; pointer-events: none; z-index: 15; overflow: hidden; }
.african-pop { position: absolute; width: 170px; max-height: 280px; object-fit: cover; border-radius: 12px; box-shadow: 0 10px 30px rgba(0,0,0,.35); opacity: 0; transform: scale(.92) rotate(var(--tilt, 0deg)); transition: opacity .5s ease-in-out, transform .5s ease-in-out; }
.african-pop.show { opacity: 1; transform: scale(1) rotate(var(--tilt, 0deg)); }
.tpl-readonly { background: var(--bg); border-radius: var(--radius); padding: .6rem .8rem; margin: .5rem 0; white-space: pre-wrap; font-size: .9rem; }
.tpl-readonly .num { color: var(--muted); font-size: .8rem; margin-right: .4rem; }

.lagos-box { position: fixed; right: 1rem; bottom: 1rem; width: 380px; z-index: 12; background: #fff4d9; border: 2px solid #102f2b; border-radius: 10px; box-shadow: 0 10px 30px rgba(0,0,0,.3); overflow: hidden; }
.lagos-head { display: flex; justify-content: space-between; align-items: center; padding: .3rem .6rem; font: 700 .75rem monospace; letter-spacing: .12em; text-transform: uppercase; color: #193e37; background: #ffbe30; border-bottom: 2px solid #102f2b; }
.lagos-head button { color: #193e37; font-size: 1rem; line-height: 1; }
.lagos-box iframe { display: block; width: 100%; aspect-ratio: 1.78; border: 0; }
.lagos-box.min iframe { display: none; }
.toasts { bottom: 1rem; }
body.african .toasts { bottom: 15.5rem; }
