:root {
  --bg: #0b1118;
  --bg-2: #0f1720;
  --surface: #131c27;
  --surface-2: #1a2533;
  --surface-3: #223042;
  --line: #243244;
  --line-2: #2f4157;
  --text: #e9eff6;
  --muted: #8a9bb0;
  --accent: #3b9eff;
  --accent-2: #2683e6;
  --accent-soft: rgba(59, 158, 255, .14);
  --danger: #ef4d56;
  --ok: #34c17a;
  --warn: #f5a524;
  --radius: 16px;
  --shadow: 0 10px 40px rgba(0, 0, 0, .45);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  color-scheme: dark;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; min-height: 100%; background: var(--bg); color: var(--text);
  font: 15px/1.5 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
body { background: radial-gradient(1200px 600px at 50% -200px, #14304d 0%, transparent 70%), var(--bg); background-attachment: fixed; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.muted { color: var(--muted); }

/* ---------- pulsanti ---------- */
.btn-primary { display: inline-flex; align-items: center; justify-content: center; gap: 8px; background: var(--accent); color: #fff; border: 0;
  border-radius: 12px; padding: 14px 18px; font-weight: 650; transition: background .15s, transform .1s; }
.btn-primary:hover { background: var(--accent-2); }
.btn-primary:active { transform: scale(.98); }
.btn-primary:disabled { opacity: .6; cursor: progress; }
.btn-primary.sm, .btn-danger.sm { padding: 8px 14px; font-size: 14px; border-radius: 10px; }
.btn-ghost { background: transparent; border: 1px solid var(--line-2); border-radius: 10px; padding: 8px 14px; }
.btn-ghost:hover { background: var(--surface-2); }
.btn-danger { display: inline-flex; align-items: center; gap: 6px; background: var(--danger); color: #fff; border: 0; border-radius: 10px; padding: 8px 14px; font-weight: 600; }
.btn-danger svg { width: 16px; height: 16px; }
.icon-btn { display: grid; place-items: center; width: 36px; height: 36px; background: transparent; border: 0; border-radius: 10px; color: var(--muted); }
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.chip { display: inline-flex; align-items: center; gap: 6px; background: var(--surface); border: 1px solid var(--line); border-radius: 999px;
  padding: 6px 12px; font-size: 13px; color: var(--muted); max-width: 55%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip:hover { color: var(--text); border-color: var(--line-2); }

/* ---------- home ---------- */
#home { max-width: 460px; margin: 0 auto; padding: calc(16px + var(--safe-t)) 16px calc(32px + var(--safe-b)); display: grid; gap: 20px; }
.topbar { display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.logo { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 17px; letter-spacing: -.01em; }
.hero h1 { margin: 8px 0 6px; font-size: 28px; line-height: 1.15; letter-spacing: -.02em; }
.hero p { margin: 0; color: var(--muted); }
.panel { background: linear-gradient(180deg, var(--surface) 0%, var(--bg-2) 100%); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; }
.connect { display: grid; gap: 14px; box-shadow: var(--shadow); }
.field { display: grid; gap: 6px; }
.field > span { font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
input, textarea, select { width: 100%; background: var(--bg); border: 1px solid var(--line-2); border-radius: 12px; padding: 12px 14px; font-size: 16px; color: var(--text); transition: border-color .15s, box-shadow .15s; }
input:focus, textarea:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 4px var(--accent-soft); }
input::placeholder { color: #55667c; }
.id-input { font: 600 28px/1.2 ui-monospace, "Cascadia Mono", Consolas, monospace; letter-spacing: .08em; text-align: center; padding: 14px; }
.pw-wrap { position: relative; }
.pw-wrap input { padding-right: 48px; }
.pw-wrap .icon-btn { position: absolute; right: 4px; top: 50%; transform: translateY(-50%); }
.check { display: flex; align-items: center; gap: 10px; font-size: 14px; color: var(--muted); cursor: pointer; }
.check input { width: 18px; height: 18px; accent-color: var(--accent); margin: 0; }
.status { margin: -4px 0 0; min-height: 1.2em; font-size: 14px; color: var(--muted); text-align: center; }
.status.err { color: var(--danger); }
.fineprint { display: flex; gap: 8px; margin: 0; font-size: 12.5px; color: var(--muted); line-height: 1.45; }
.fineprint svg { width: 16px; height: 16px; margin-top: 1px; color: var(--ok); }

.section-head h2 { margin: 0 0 10px; font-size: 13px; font-weight: 600; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
#recentList, #deviceList { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 10px; }
.dev { position: relative; display: flex; align-items: center; gap: 12px; padding: 12px; background: var(--surface); border: 1px solid var(--line);
  border-radius: 14px; cursor: pointer; transition: border-color .15s, background .15s; text-align: left; width: 100%; }
.dev:hover { border-color: var(--line-2); background: var(--surface-2); }
.dev .avatar { width: 40px; height: 40px; border-radius: 11px; display: grid; place-items: center; background: var(--accent-soft); color: var(--accent); flex: none; }
.dev .meta { min-width: 0; flex: 1; }
.dev b { display: block; font-size: 14.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.dev small { color: var(--muted); font: 12.5px ui-monospace, Consolas, monospace; display: flex; align-items: center; gap: 6px; }
.dev .saved { color: var(--ok); font-family: inherit; }
.dev .del { position: absolute; top: 4px; right: 4px; width: 28px; height: 28px; opacity: 0; }
.dev:hover .del, .dev:focus-within .del { opacity: 1; }
@media (pointer: coarse) { .dev .del { opacity: .7; } }
.dev .del svg { width: 15px; height: 15px; }

.top-actions { display: flex; gap: 8px; min-width: 0; justify-content: flex-end; }
.top-actions .chip { max-width: none; }
#btnWho { max-width: 170px; }
.chip.account svg { width: 15px; height: 15px; }
.chip.account.logged { color: var(--text); border-color: rgba(59, 158, 255, .45); background: var(--accent-soft); }
@media (max-width: 420px) { #btnWho { display: none; } }
.row-between { display: flex; justify-content: space-between; align-items: center; }
.row-between h2 { margin: 0; }
.small { font-size: 13px; margin: 0; }
.dev .avatar { position: relative; }
.dev .online { position: absolute; right: -3px; bottom: -3px; width: 12px; height: 12px; border-radius: 50%; background: #55667c; border: 2px solid var(--surface); }
.dev .online.on { background: var(--ok); }
.dev.offline { opacity: .7; }
.seg { display: flex; gap: 4px; padding: 4px; background: var(--bg); border: 1px solid var(--line); border-radius: 12px; margin-bottom: 12px; }
.seg button { flex: 1; padding: 8px; border: 0; border-radius: 9px; background: transparent; color: var(--muted); font-weight: 600; }
.seg button.active { background: var(--surface-3); color: var(--text); }
#accForm { display: grid; gap: 12px; }
#accForm .row { margin-top: 0; }
.btn-danger.sm { padding: 8px 14px; font-size: 14px; }
.download { display: grid; gap: 14px; }
.dl-head { display: flex; gap: 12px; align-items: center; }
.dl-head .avatar { width: 42px; height: 42px; border-radius: 12px; display: grid; place-items: center; background: var(--accent-soft); color: var(--accent); flex: none; }
.dl-head b { display: block; font-size: 15.5px; }
.dl-head small { color: var(--muted); font-size: 13px; line-height: 1.4; display: block; }
#dlBody { display: grid; gap: 10px; }
.dl-main { display: flex; justify-content: space-between; text-decoration: none; }
.dl-main span:last-child { font-weight: 500; opacity: .85; font-size: 13px; }
.dl-alt { color: var(--accent); font-size: 14px; text-decoration: none; justify-self: start; }
.dl-alt:hover { text-decoration: underline; }
.dl-note { margin: 0; font-size: 12.5px; color: var(--muted); line-height: 1.45; }
.dl-note b { color: var(--text); }
.dl-sha summary { cursor: pointer; font-size: 12px; color: var(--muted); }
.dl-sha code { display: block; margin-top: 6px; font: 11px/1.4 ui-monospace, Consolas, monospace; color: var(--muted); word-break: break-all; user-select: all; }
.dl-copy { display: flex; gap: 8px; }
.dl-copy input { font: 14px ui-monospace, Consolas, monospace; }
.install summary { cursor: pointer; font-weight: 600; list-style: none; display: flex; justify-content: space-between; }
.install summary::after { content: '+'; color: var(--muted); font-weight: 400; }
.install[open] summary::after { content: '−'; }
.install ol { margin: 12px 0 0; padding-left: 20px; color: var(--muted); display: grid; gap: 6px; font-size: 14px; }
.install b { color: var(--text); }

/* ---------- sessione ---------- */
#session { position: fixed; inset: 0; background: #000; overflow: hidden; }
#stage { position: absolute; inset: 0; touch-action: none; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }
#screen, #iosCanvas { width: 100%; height: 100%; object-fit: contain; display: block; transform-origin: center; }
#cursor { position: absolute; width: 20px; height: 20px; margin: -2px 0 0 -2px; pointer-events: none; filter: drop-shadow(0 1px 2px rgba(0,0,0,.6));
  background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 18 18'%3E%3Cpath d='M2 2l0 13 3.5-3.5 2.5 5 2-1-2.5-5H13z' fill='%23fff' stroke='%23000' stroke-width='1.2'/%3E%3C/svg%3E") no-repeat; }

.overlay { position: absolute; inset: 0; display: grid; place-items: center; background: radial-gradient(800px 400px at 50% 30%, #14304d 0%, transparent 70%), var(--bg); z-index: 20; padding: 20px; }
.ov-card { display: grid; justify-items: center; gap: 14px; text-align: center; max-width: 340px; }
.ov-card b { font-size: 18px; }
.ov-card p { margin: 0; font-size: 14px; }
.spinner { width: 44px; height: 44px; border: 3px solid var(--line-2); border-top-color: var(--accent); border-radius: 50%; animation: spin .9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.steps { display: flex; gap: 6px; list-style: none; margin: 0; padding: 0; font-size: 12px; }
.steps li { padding: 4px 10px; border-radius: 999px; background: var(--surface-2); color: var(--muted); border: 1px solid var(--line); }
.steps li.active { color: var(--text); border-color: var(--accent); background: var(--accent-soft); }
.steps li.done { color: var(--ok); border-color: rgba(52, 193, 122, .35); }
.steps li.done::before { content: '✓ '; }

/* barra desktop */
#hotzone { position: absolute; top: 0; left: 0; right: 0; height: 8px; z-index: 9; }
.bar { position: absolute; top: calc(10px + var(--safe-t)); left: 50%; transform: translate(-50%, 0); z-index: 10; display: flex; align-items: center; gap: 2px;
  padding: 5px; background: rgba(19, 28, 39, .88); backdrop-filter: blur(14px) saturate(1.4); -webkit-backdrop-filter: blur(14px) saturate(1.4);
  border: 1px solid var(--line-2); border-radius: 14px; box-shadow: var(--shadow); transition: transform .25s ease, opacity .25s; max-width: calc(100vw - 20px); }
.bar.hide { transform: translate(-50%, calc(-100% - 20px)); opacity: 0; pointer-events: none; }
.bar .who { display: flex; align-items: center; gap: 8px; padding: 0 10px 0 6px; min-width: 0; }
.bar .who b { font-size: 13.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 170px; }
.bar .who small { font: 11.5px ui-monospace, Consolas, monospace; color: var(--muted); white-space: nowrap; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--ok); box-shadow: 0 0 0 3px rgba(52, 193, 122, .2); flex: none; }
.dot.warn { background: var(--warn); box-shadow: 0 0 0 3px rgba(245, 165, 36, .2); }
.sep { width: 1px; height: 22px; background: var(--line-2); margin: 0 4px; }
.tb { position: relative; display: inline-flex; align-items: center; gap: 6px; height: 36px; min-width: 36px; padding: 0 9px; background: transparent; border: 0;
  border-radius: 9px; color: #c4d0de; font-size: 13px; }
.tb:hover { background: var(--surface-3); color: #fff; }
.tb.on { background: var(--accent-soft); color: var(--accent); }
.tb.danger { color: #fff; background: var(--danger); margin-left: 4px; }
.tb.danger:hover { background: #d83c45; }
.tb svg { width: 18px; height: 18px; }
.tb[data-tip]:hover::after { content: attr(data-tip); position: absolute; top: calc(100% + 8px); left: 50%; transform: translateX(-50%); white-space: nowrap;
  background: #000; color: #fff; font-size: 12px; padding: 4px 8px; border-radius: 6px; pointer-events: none; }
/* finestre strette: solo icone */
@media (max-width: 860px) {
  .bar .tb span:not(.badge) { display: none; }
  .bar .tb.danger span { display: inline; }
  .bar .who small { display: none; }
  .bar .who b { max-width: 110px; }
}
@media (max-width: 600px) {
  .bar .who, .bar .who + .sep { display: none; }
  .bar .tb.danger span { display: none; }
  .tb { min-width: 34px; padding: 0 7px; }
}
.badge { position: absolute; top: 2px; right: 2px; min-width: 16px; height: 16px; padding: 0 4px; border-radius: 8px; background: var(--danger); color: #fff;
  font-size: 10px; font-weight: 700; display: grid; place-items: center; }

/* telefono: pulsante flottante + bottom sheet */
.fab { position: absolute; right: 14px; bottom: calc(18px + var(--safe-b)); z-index: 10; width: 52px; height: 52px; border-radius: 50%; border: 1px solid var(--line-2);
  background: rgba(19, 28, 39, .82); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px); color: #fff; display: grid; place-items: center; box-shadow: var(--shadow);
  touch-action: none; transition: opacity .2s; }
.fab.dim { opacity: .45; }
.fab svg { width: 22px; height: 22px; }
.fab .badge { top: -2px; right: -2px; }
#sheetBackdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, .45); z-index: 14; }
.sheet { position: absolute; left: 0; right: 0; bottom: 0; z-index: 15; background: var(--surface); border-top: 1px solid var(--line-2);
  border-radius: 22px 22px 0 0; padding: 8px 16px calc(16px + var(--safe-b)); box-shadow: 0 -10px 40px rgba(0, 0, 0, .5);
  animation: up .22s ease; max-height: 85%; overflow-y: auto; }
@keyframes up { from { transform: translateY(40%); opacity: 0; } }
.grabber { width: 40px; height: 4px; border-radius: 2px; background: var(--line-2); margin: 0 auto 12px; }
.sheet-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 14px; }
.sheet-head b { display: block; font-size: 16px; }
.sheet-head small { font: 12px ui-monospace, Consolas, monospace; }
.sheet-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
@media (min-width: 520px) { .sheet { max-width: 520px; margin: 0 auto; } .sheet-grid { grid-template-columns: repeat(5, 1fr); } }
.tile { position: relative; display: grid; justify-items: center; gap: 6px; padding: 12px 4px 10px; background: var(--surface-2); border: 1px solid transparent;
  border-radius: 14px; font-size: 11.5px; color: #c4d0de; line-height: 1.2; text-align: center; }
.tile svg { width: 22px; height: 22px; color: var(--text); }
.tile.on { background: var(--accent-soft); border-color: rgba(59, 158, 255, .4); color: var(--accent); }
.tile.on svg { color: var(--accent); }
.tile:active { transform: scale(.96); }
.tile .badge { top: 6px; right: 10px; }

/* popover (menu) — desktop sotto il pulsante, telefono come foglio */
.popover { position: absolute; z-index: 16; min-width: 220px; background: var(--surface); border: 1px solid var(--line-2); border-radius: 14px; padding: 6px;
  box-shadow: var(--shadow); animation: pop .14s ease; }
@keyframes pop { from { opacity: 0; transform: translateY(-4px); } }
.popover h4 { margin: 4px 8px 6px; font-size: 11.5px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.popover .opt { display: flex; align-items: center; gap: 10px; width: 100%; padding: 9px 10px; background: none; border: 0; border-radius: 9px; text-align: left; font-size: 14px; }
.popover .opt:hover { background: var(--surface-3); }
.popover .opt small { color: var(--muted); margin-left: auto; font-size: 12px; }
.popover .opt .tick { width: 16px; height: 16px; color: var(--accent); visibility: hidden; }
.popover .opt.sel .tick { visibility: visible; }
.popover .keys { display: grid; grid-template-columns: repeat(2, 1fr); gap: 4px; width: 280px; }
.popover .keys .opt { background: var(--surface-2); justify-content: space-between; font-size: 13px; }
.popover .keys .opt kbd { font: 11px ui-monospace, Consolas, monospace; color: var(--muted); }
.popover.as-sheet { left: 0 !important; right: 0; top: auto !important; bottom: 0; border-radius: 22px 22px 0 0; padding: 14px 12px calc(14px + var(--safe-b)); max-height: 75%; overflow-y: auto; }
.popover.as-sheet .keys { width: auto; }

/* riga tasti sopra la tastiera del telefono */
.keybar { position: absolute; left: 0; right: 0; bottom: 0; z-index: 12; display: flex; gap: 6px; overflow-x: auto; padding: 8px; scrollbar-width: none;
  background: rgba(19, 28, 39, .96); border-top: 1px solid var(--line-2); }
.keybar::-webkit-scrollbar { display: none; }
.keybar button { flex: none; min-width: 46px; height: 38px; padding: 0 10px; background: var(--surface-3); border: 1px solid transparent; border-radius: 9px; font-size: 14px; }
.keybar button.latched { background: var(--accent); color: #fff; }
.keybar .kb-close { margin-left: auto; background: transparent; border-color: var(--line-2); }
#kbInput { position: absolute; left: 0; bottom: 0; width: 1px; height: 1px; opacity: 0; border: 0; padding: 0; font-size: 16px; }

/* chat */
.chat { position: absolute; right: 12px; top: 64px; bottom: calc(12px + var(--safe-b)); width: min(360px, calc(100% - 24px)); z-index: 13;
  background: var(--surface); border: 1px solid var(--line-2); border-radius: 18px; display: flex; flex-direction: column; box-shadow: var(--shadow); }
@media (pointer: coarse) { .chat { top: auto; height: min(460px, 70%); } }
.chat header { display: flex; justify-content: space-between; align-items: center; padding: 8px 8px 8px 16px; border-bottom: 1px solid var(--line); }
#chatLog { flex: 1; overflow-y: auto; list-style: none; margin: 0; padding: 12px; display: flex; flex-direction: column; gap: 6px; }
#chatLog li { max-width: 82%; padding: 8px 12px; border-radius: 14px 14px 14px 4px; background: var(--surface-3); word-wrap: break-word; white-space: pre-wrap; font-size: 14px; }
#chatLog li.me { align-self: flex-end; background: var(--accent-2); border-radius: 14px 14px 4px 14px; }
#chatLog li.empty { align-self: center; background: none; color: var(--muted); font-size: 13px; text-align: center; }
#chatForm { display: flex; gap: 8px; padding: 10px; border-top: 1px solid var(--line); }
#chatForm input { padding: 10px 12px; }

/* trasferimenti, toast, dialoghi */
#transfers { position: absolute; left: 12px; bottom: calc(12px + var(--safe-b)); display: grid; gap: 8px; z-index: 13; width: min(320px, calc(100% - 90px)); }
.xfer { display: grid; gap: 6px; background: var(--surface); border: 1px solid var(--line-2); border-radius: 12px; padding: 10px 12px; font-size: 13px; box-shadow: var(--shadow); }
.xfer .lbl { display: flex; gap: 8px; align-items: center; overflow: hidden; }
.xfer .lbl span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.xfer .bar-p { height: 4px; border-radius: 2px; background: var(--surface-3); overflow: hidden; }
.xfer .bar-p i { display: block; height: 100%; width: 0; background: var(--accent); transition: width .2s; }
.xfer a { color: var(--accent); font-weight: 600; text-decoration: none; }
#toast { position: absolute; top: calc(70px + var(--safe-t)); left: 50%; transform: translateX(-50%); z-index: 30; background: var(--surface); border: 1px solid var(--line-2);
  border-radius: 12px; padding: 10px 16px; box-shadow: var(--shadow); max-width: calc(100% - 32px); font-size: 14px; animation: pop .18s ease; }
dialog { background: var(--surface); color: var(--text); border: 1px solid var(--line-2); border-radius: 18px; width: min(440px, calc(100% - 32px)); padding: 20px; box-shadow: var(--shadow); }
dialog::backdrop { background: rgba(0, 0, 0, .6); }
dialog h3 { margin: 0 0 8px; }
dialog p { margin: 0 0 12px; font-size: 14px; }
dialog .row { display: flex; gap: 8px; justify-content: flex-end; margin-top: 14px; }
