:root {
  --bg: #f5f4f1;
  --surface: #ffffff;
  --surface-2: #f0eee9;
  --text: #1d1b18;
  --muted: #6d6860;
  --border: #e3e0d9;
  --accent: #ff5a36;
  --accent-ink: #ffffff;
  --accent-soft: #ffe7e0;
  --ok: #1f8a4c;
  --ok-soft: #dcf3e5;
  --danger: #c62828;
  --danger-soft: #fde3e3;
  --warn-soft: #fff4d6;
  --radius: 14px;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.04), 0 4px 16px rgb(0 0 0 / 0.05);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141311;
    --surface: #1e1c1a;
    --surface-2: #282624;
    --text: #f1eee9;
    --muted: #a39d94;
    --border: #34312d;
    --accent-soft: #4a2219;
    --ok: #4cc27f;
    --ok-soft: #173826;
    --danger: #ff6b6b;
    --danger-soft: #431b1b;
    --warn-soft: #3d3215;
    --shadow: 0 1px 2px rgb(0 0 0 / 0.3), 0 4px 16px rgb(0 0 0 / 0.25);
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.45;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }
[hidden] { display: none !important; }

/* Topbar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 800;
  font-size: 1.15rem;
  letter-spacing: -0.01em;
}

.brand-sub { font-weight: 500; color: var(--muted); font-size: 0.9rem; }

.logo {
  display: inline-grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border-radius: 9px;
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 900;
}

.session { display: flex; align-items: center; gap: 8px; min-width: 0; }
.who { color: var(--muted); font-size: 0.9rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 40vw; }

.badge {
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
}

/* Layout */
main { max-width: 1120px; margin: 0 auto; padding: 20px 16px 48px; }

.view.center { display: grid; place-items: center; min-height: 70vh; }

.grid { display: grid; gap: 20px; grid-template-columns: 1fr; }
.grid.two { grid-template-columns: 1fr; }

@media (min-width: 861px) {
  .grid.two { grid-template-columns: 1fr 1fr; align-items: start; }
  .grid:not(.two) { max-width: 640px; margin: 0 auto; }
}

/* Card */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
  min-width: 0;
}

.card-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

h1, h2 { margin: 0; letter-spacing: -0.01em; }
h1 { font-size: 1.6rem; }
h2 { font-size: 1.2rem; }

.muted { color: var(--muted); }
.small-text { font-size: 0.88rem; margin: 2px 0 0; }

.login-card { width: 100%; max-width: 380px; display: grid; gap: 14px; }
.login-card p { margin: 0; }

/* Form */
label { display: grid; gap: 6px; font-size: 0.9rem; font-weight: 600; color: var(--muted); }

input {
  width: 100%;
  font: inherit;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 11px 12px;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}

input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.inline-form {
  display: grid;
  gap: 12px;
  padding: 14px;
  margin-bottom: 14px;
  border-radius: 12px;
  background: var(--surface-2);
  border: 1px dashed var(--border);
}

.inline-form input { background: var(--surface); }

.row { display: flex; gap: 8px; align-items: center; }
.row.end { justify-content: flex-end; flex-wrap: wrap; }
.row input { flex: 1; min-width: 0; }

.error { color: var(--danger); font-size: 0.9rem; margin: 0; }
.warn {
  background: var(--warn-soft);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 0.9rem;
  margin: 0;
}

/* Bottoni */
.btn {
  font: inherit;
  font-weight: 650;
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 10px 16px;
  cursor: pointer;
  white-space: nowrap;
  transition: transform 0.05s, background 0.15s, opacity 0.15s;
  color: var(--text);
  background: transparent;
}

.btn:active { transform: scale(0.98); }
.btn:disabled { opacity: 0.55; cursor: default; }
.btn.small { padding: 7px 12px; font-size: 0.88rem; }
.btn.block { width: 100%; }

.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.primary:hover:not(:disabled) { filter: brightness(1.05); }

.btn.ghost { border-color: var(--border); background: var(--surface); }
.btn.ghost:hover:not(:disabled) { background: var(--surface-2); }

.btn.danger { color: var(--danger); border-color: var(--danger-soft); background: var(--danger-soft); }

.btn.link {
  background: none;
  border: none;
  color: var(--muted);
  font-weight: 500;
  font-size: 0.9rem;
  padding: 4px;
  text-decoration: underline;
}

/* Liste */
.list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.empty { margin: 8px 0 0; }

.item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
}

.item.clickable { cursor: pointer; transition: border-color .15s, background-color .15s; }
.item.clickable:hover:not(.selected) { border-color: var(--accent); }
.item.clickable:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.selected-tag { color: var(--accent); font-weight: 700; font-size: 0.85rem; white-space: nowrap; }
.item.selected { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-soft); }
.item.revoked { opacity: 0.6; }

.item-icon { font-size: 1.5rem; }
.item-main { flex: 1; min-width: 0; }
.item-title { font-weight: 650; display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
.item-sub { color: var(--muted); font-size: 0.9rem; overflow-wrap: anywhere; }
.item-actions { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.item-meta { color: var(--muted); font-size: 0.8rem; margin-top: 2px; }

.chip {
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  padding: 1px 7px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
}
.chip.accent { background: var(--accent-soft); color: var(--accent); }
.chip.ok { background: var(--ok-soft); color: var(--ok); }
.chip.danger { background: var(--danger-soft); color: var(--danger); }

/* Dialog chiave */
dialog {
  border: none;
  border-radius: 18px;
  padding: 0;
  width: min(440px, calc(100vw - 32px));
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 20px 60px rgb(0 0 0 / 0.3);
}

dialog::backdrop { background: rgb(0 0 0 / 0.45); }

.dialog-body { padding: 24px; display: grid; gap: 14px; text-align: center; }
.dialog-body .row { justify-content: center; }
.dialog-body p { margin: 0; }

.key-display {
  font-family: var(--mono);
  font-size: clamp(1.6rem, 7vw, 2.4rem);
  font-weight: 800;
  letter-spacing: 0.06em;
  padding: 18px 10px;
  border-radius: 12px;
  background: var(--accent-soft);
  color: var(--accent);
  user-select: all;
  overflow-wrap: anywhere;
}

.qr { display: grid; place-items: center; }
.qr:empty { display: none; }
.qr img { background: #fff; padding: 8px; border-radius: 10px; image-rendering: pixelated; }

/* Toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: 20px;
  transform: translateX(-50%);
  max-width: calc(100vw - 32px);
  background: var(--text);
  color: var(--bg);
  padding: 12px 18px;
  border-radius: 12px;
  box-shadow: var(--shadow);
  font-size: 0.95rem;
  z-index: 50;
}
.toast.ok { background: var(--ok); color: #fff; }
.toast.err { background: var(--danger); color: #fff; }

/* Spinner */
.spinner {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Mobile */
@media (max-width: 520px) {
  .card { padding: 16px; }
  .brand-sub { display: none; }
  .item { flex-wrap: wrap; }
  .item .btn { margin-left: auto; }
}

.hint { color: var(--muted); font-size: 0.85rem; margin: 0; }

.list-tools { display: flex; gap: 8px; margin-bottom: 12px; }
.list-tools input { flex: 1 1 auto; min-width: 0; }
.list-tools select {
  width: auto; flex: 0 0 auto;
  font: inherit; padding: 10px 12px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
}
@media (max-width: 520px) { .list-tools { flex-direction: column; } .list-tools select { width: 100%; } }

.pass-rules { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; font-size: 0.88rem; color: var(--muted); }
.pass-rules li::before { content: "○ "; }
.pass-rules li.ok { color: var(--ok); }
.pass-rules li.ok::before { content: "✓ "; }
a.btn { text-decoration: none; text-align: center; }

/* Interruttore foto, in alto a destra nella riga utente */
.photo-toggle {
  margin-left: auto;
  display: inline-flex; align-items: center; gap: 8px;
  font: inherit; font-size: 0.85rem; font-weight: 700;
  padding: 5px 10px 5px 12px; border-radius: 999px; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface-2); color: var(--muted);
}
.photo-toggle .switch {
  position: relative; width: 34px; height: 20px; border-radius: 999px;
  background: var(--border); transition: background-color .15s;
}
.photo-toggle .switch::after {
  content: ""; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px;
  border-radius: 50%; background: #fff; box-shadow: 0 1px 2px rgba(0,0,0,.25); transition: transform .15s;
}
.photo-toggle.on { border-color: var(--ok); background: var(--ok-soft); color: var(--ok); }
.photo-toggle.on .switch { background: var(--ok); }
.photo-toggle.on .switch::after { transform: translateX(14px); }
.photo-toggle-state { min-width: 1.4em; }
.photo-toggle:disabled { opacity: .6; cursor: wait; }

/* Riga utente: titolo + interruttore foto su tutta la larghezza, azioni sotto a destra */
.item.user { flex-wrap: wrap; }
.item.user .item-main { flex: 1 1 100%; }
.item.user .item-actions { flex: 1 1 100%; align-items: center; }

/* Condivisioni */
.shares-card { margin-top: 20px; }
.shares-card .card-head { align-items: flex-start; }
.share-arrow { color: var(--accent); font-weight: 800; margin: 0 4px; align-self: center; }
.share-lists { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.list-pill {
  font: inherit; font-size: 0.85rem; font-weight: 700; cursor: pointer;
  padding: 5px 12px; border-radius: 999px;
  border: 1px dashed var(--border); background: transparent; color: var(--muted);
}
.list-pill.on { border-style: solid; border-color: var(--accent); background: var(--accent-soft); color: var(--accent); }
.list-pill:disabled { opacity: .6; cursor: wait; }
.row.wrap { flex-wrap: wrap; align-items: flex-end; }
.grow { flex: 1 1 200px; }
select {
  width: 100%; font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px;
}
.list-choice { border: 1px solid var(--border); border-radius: 10px; padding: 8px 12px 12px; display: flex; flex-wrap: wrap; gap: 14px; }
.list-choice legend { font-size: 0.9rem; font-weight: 600; color: var(--muted); padding: 0 4px; }
.list-choice label { display: inline-flex; align-items: center; gap: 6px; color: var(--text); font-weight: 600; }
.list-choice input { width: auto; }
.item.share { flex-wrap: wrap; }
.item.share .item-main { flex: 1 1 260px; }

.chip.warn { background: rgba(255, 159, 10, .16); color: #c27600; }
.item.key.disabled { opacity: .75; }
.item.key.disabled .item-icon { filter: grayscale(1); }

/* Pulsante "i" e riquadro flottante con le date del dispositivo */
.info-btn {
  flex: none; width: 20px; height: 20px; border-radius: 50%; padding: 0; cursor: pointer;
  display: inline-grid; place-items: center;
  font: 700 12px/1 -apple-system, system-ui, sans-serif;
  border: 0; background: var(--surface-2); color: var(--muted);
}
.info-btn:hover { border-color: var(--accent); color: var(--accent); }
.info-pop {
  margin: auto; border: 1px solid var(--border); border-radius: 16px; padding: 18px 20px;
  background: var(--surface); color: var(--text); box-shadow: 0 16px 40px rgba(0,0,0,.25); min-width: 260px;
}
.info-pop::backdrop { background: rgba(0,0,0,.2); }
.info-pop-title { font-weight: 700; margin-bottom: 10px; }
.info-pop dl { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; margin: 0; font-size: .92rem; }
.info-pop dt { color: var(--muted); }
.info-pop dd { margin: 0; }


/* Online / offline dei dispositivi */
.online { display: inline-flex; align-items: center; gap: 5px; font-size: .78rem; font-weight: 700; color: var(--muted); }
.online::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--muted); opacity: .6; }
.online.on { color: var(--ok); }
.online.on::before { background: var(--ok); opacity: 1; box-shadow: 0 0 0 3px var(--ok-soft); }

/* Riga dispositivo: nome e tag sopra, azioni sotto a destra */
.item.key { flex-wrap: wrap; align-items: flex-start; }
.item.key .item-main { flex: 1 1 0; min-width: 0; }
.item.key .item-actions { flex: 1 1 100%; justify-content: flex-end; }
.key-name { display: flex; align-items: center; gap: 8px; font-weight: 650; }
.key-tags { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin-top: 6px; }
