:root {
  --bg: #f4f6fb;
  --surface: #ffffff;
  --surface-2: #eef1f7;
  --text: #0f172a;
  --muted: #5b6577;
  --border: #d9dee8;
  --primary: #2563eb;
  --primary-text: #ffffff;
  --danger: #dc2626;
  --ok: #16a34a;
  --warn: #d97706;
  --radius: 14px;
  --shadow: 0 6px 24px rgba(15, 23, 42, .08);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1120;
    --surface: #131c2e;
    --surface-2: #1b263b;
    --text: #e6ebf5;
    --muted: #94a0b8;
    --border: #2a364d;
    --primary: #3b82f6;
    --danger: #ef4444;
    --ok: #22c55e;
    --warn: #f59e0b;
    --shadow: 0 6px 24px rgba(0, 0, 0, .4);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
[hidden] { display: none !important; }
h1 { font-size: 1.5rem; margin: .25rem 0; }
h2 { font-size: 1.15rem; margin: 0; }
h3 { font-size: .95rem; margin: .75rem 0 0; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
code { background: var(--surface-2); padding: 1px 5px; border-radius: 5px; }

.screen { min-height: 100%; padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left); }
.center { display: grid; place-items: center; padding: 16px; }
.center-text { text-align: center; padding: 32px 16px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 24px; width: 100%; }
.narrow { max-width: 420px; text-align: center; }
.narrow .stack, .narrow form { text-align: left; }
.stack { display: flex; flex-direction: column; gap: 12px; }
.row { display: flex; gap: 8px; align-items: center; }
.wrap { flex-wrap: wrap; }
.grow { flex: 1; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.error { color: var(--danger); margin: 0; }
.warn { font-size: .85rem; background: color-mix(in srgb, var(--warn) 14%, transparent); border-radius: 10px; padding: 10px 12px; margin: 0; }

.logo {
  width: 64px; height: 64px; margin: 0 auto 4px;
  background: url("../icons/icon.svg") center / contain no-repeat;
}

label { display: flex; flex-direction: column; gap: 6px; font-size: .9rem; color: var(--muted); }
label.check { flex-direction: row; align-items: center; gap: 8px; color: var(--text); }
label.inline { flex-direction: row; align-items: center; gap: 8px; color: var(--text); }
input, textarea, select {
  font: inherit; color: var(--text);
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px;
  padding: 11px 12px; width: 100%; min-width: 0;
}
input[type=checkbox] { width: 18px; height: 18px; accent-color: var(--primary); }
input[type=number] { width: 80px; }
label.inline select { width: auto; }
input:focus, textarea:focus, select:focus { outline: 2px solid var(--primary); outline-offset: -1px; }
textarea { resize: vertical; }
.input-row { display: flex; gap: 6px; align-items: center; }
.gen-opts { display: flex; gap: 16px; align-items: center; flex-wrap: wrap; font-size: .9rem; }

.meter { height: 6px; background: var(--surface-2); border-radius: 3px; overflow: hidden; margin-top: -4px; }
.meter > div { height: 100%; width: 0; background: var(--danger); transition: width .2s; }
.meter > div[data-level=mid] { background: var(--warn); }
.meter > div[data-level=high] { background: var(--ok); }

.btn {
  font: inherit; font-weight: 600; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text);
  border-radius: 10px; padding: 11px 16px;
}
.btn:disabled { opacity: .55; cursor: default; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.btn.danger { background: transparent; border-color: var(--danger); color: var(--danger); }
#btn-restore { width: 100%; }
.icon-btn {
  font-size: 1.15rem; line-height: 1; cursor: pointer; flex: none;
  width: 40px; height: 40px; border-radius: 10px;
  border: none; background: transparent; color: var(--text);
}
.icon-btn:hover { background: var(--surface-2); }

.divider { display: flex; align-items: center; gap: 10px; color: var(--muted); margin: 18px 0; font-size: .85rem; }
.divider::before, .divider::after { content: ""; flex: 1; height: 1px; background: var(--border); }

/* Barra superiore */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 6px;
  padding: 10px 12px; background: var(--bg);
  border-bottom: 1px solid var(--border);
}
.brand { font-size: 1.1rem; flex: 1; margin: 0; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; cursor: pointer;
  font: inherit; font-size: .8rem; color: var(--muted);
  background: var(--surface); border: 1px solid var(--border); border-radius: 999px; padding: 6px 10px;
  max-width: 55vw; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; }
.dot[data-state=ok] { background: var(--ok); }
.dot[data-state=warn] { background: var(--warn); }
.dot[data-state=err] { background: var(--danger); }
.dot[data-state=busy] { background: var(--primary); animation: pulse 1s infinite alternate; }
@keyframes pulse { to { opacity: .3; } }

.searchbar { padding: 12px; max-width: 760px; margin: 0 auto; display: flex; gap: 8px; }
.searchbar #sort { width: auto; flex: none; }

.list { list-style: none; margin: 0 auto; padding: 0 12px 96px; max-width: 760px; }
.item {
  display: flex; align-items: center; gap: 4px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  margin-bottom: 8px; padding-right: 6px;
}
.item-main {
  flex: 1; min-width: 0; display: flex; align-items: center; gap: 12px;
  background: none; border: 0; color: inherit; font: inherit; text-align: left; cursor: pointer;
  padding: 12px;
}
.avatar {
  width: 38px; height: 38px; border-radius: 10px; flex: none;
  display: grid; place-items: center; font-weight: 700;
  background: color-mix(in srgb, var(--primary) 18%, transparent); color: var(--primary);
}
.item-text { display: flex; flex-direction: column; min-width: 0; }
.item-title, .item-sub { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item-title { font-weight: 600; }
.item-sub { font-size: .85rem; color: var(--muted); }

.fab {
  position: fixed; right: calc(20px + env(safe-area-inset-right)); bottom: calc(20px + env(safe-area-inset-bottom));
  width: 58px; height: 58px; border-radius: 50%; border: 0; cursor: pointer;
  background: var(--primary); color: #fff; font-size: 2rem; line-height: 1;
  box-shadow: 0 8px 24px rgba(37, 99, 235, .45);
}

/* Dialoghi */
dialog {
  border: 1px solid var(--border); border-radius: var(--radius); padding: 0;
  background: var(--surface); color: var(--text); box-shadow: var(--shadow);
  width: min(520px, calc(100vw - 24px)); max-height: calc(100dvh - 24px);
}
dialog::backdrop { background: rgba(2, 6, 23, .55); }
.dlg { padding: 16px 18px 18px; display: flex; flex-direction: column; gap: 14px; }
.dlg-head { display: flex; align-items: center; gap: 8px; }
.dlg-head h2 { flex: 1; overflow-wrap: anywhere; }
.dlg-foot { display: flex; gap: 8px; align-items: center; margin-top: 4px; }

.fields { display: flex; flex-direction: column; gap: 4px; }
.field {
  display: flex; align-items: center; gap: 6px;
  background: var(--surface-2); border-radius: 10px; padding: 6px 6px 6px 12px;
}
.field.col { flex-direction: column; align-items: stretch; padding: 10px 12px; }
.lbl { font-size: .75rem; color: var(--muted); width: 64px; flex: none; }
.field.col .lbl { width: auto; }
.val { flex: 1; min-width: 0; overflow-wrap: anywhere; color: var(--text); }
a.val { color: var(--primary); text-decoration: none; }
.mono { font-family: ui-monospace, "SF Mono", Consolas, monospace; }
.notes { margin: 4px 0 0; white-space: pre-wrap; font: inherit; }

.toast {
  position: fixed; left: 50%; bottom: calc(92px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: #0f172a; color: #fff; padding: 10px 16px; border-radius: 10px; z-index: 100;
  font-size: .9rem; max-width: calc(100vw - 32px); box-shadow: var(--shadow);
}
@media (prefers-color-scheme: dark) { .toast { background: #e6ebf5; color: #0f172a; } }

/* Soggetti e categorie */
.filters { max-width: 760px; margin: 0 auto; padding: 0 12px 8px; display: flex; flex-direction: column; gap: 6px; }
.chips-row { display: flex; align-items: center; gap: 4px; }
.chips {
  display: flex; gap: 6px; overflow-x: auto; flex: 1; min-width: 0;
  scrollbar-width: none; -webkit-overflow-scrolling: touch; padding: 2px 0;
}
.chips::-webkit-scrollbar { display: none; }
.fchip {
  flex: none; font: inherit; font-size: .88rem; cursor: pointer; white-space: nowrap;
  padding: 7px 12px; border-radius: 999px;
  background: var(--surface); color: var(--text); border: 1px solid var(--border);
}
.fchip.active { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.fchip.add { color: var(--primary); border-style: dashed; background: transparent; }
.fchip.ghost { color: var(--muted); font-style: italic; }
.fchip.active.ghost { color: var(--primary-text); }
.chips.sub .fchip { font-size: .82rem; padding: 5px 10px; }
.chips.sub .fchip.active { background: color-mix(in srgb, var(--primary) 20%, var(--surface)); color: var(--primary); border-color: var(--primary); }

.group {
  font-size: .78rem; font-weight: 700; color: var(--muted);
  text-transform: uppercase; letter-spacing: .04em; margin: 14px 4px 6px;
}
.group:first-child { margin-top: 2px; }

.path { margin: -8px 0 0; font-size: .85rem; color: var(--primary); }
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
@media (max-width: 380px) { .two-col { grid-template-columns: 1fr; } }
select:disabled { opacity: .6; }

.tree { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 10px; max-height: 55dvh; overflow-y: auto; }
.tree-subject { background: var(--surface-2); border-radius: 12px; padding: 6px 6px 8px 12px; }
.tree-row { display: flex; align-items: center; gap: 6px; }
.tree-name { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.strong { font-weight: 700; }
.tree-cats { list-style: none; margin: 0; padding: 0 0 0 14px; border-left: 2px solid var(--border); }
.tree-cats .icon-btn { width: 34px; height: 34px; font-size: 1rem; }
.link-btn { font: inherit; font-size: .88rem; color: var(--primary); background: none; border: 0; cursor: pointer; padding: 8px 0; }

/* Modalità riservata */
#btn-privacy[aria-pressed=true] { background: color-mix(in srgb, var(--ok) 22%, transparent); }
body.privacy .topbar { box-shadow: inset 0 -3px 0 var(--ok); }
body.privacy #d-user, body.privacy #d-pwd, body.privacy #d-notes, body.privacy #d-reveal, body.privacy #e-reveal {
  user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; touch-action: none; cursor: pointer;
}
body.privacy .peeking { outline: 2px dashed var(--warn); outline-offset: 2px; border-radius: 4px; }
/* nel modulo: utente e note coperti finché non si tiene premuto 👁 */
body.privacy:not(.peek) #e-user, body.privacy:not(.peek) #e-notes { -webkit-text-security: disc; }

body.shielded .screen, body.shielded dialog { visibility: hidden; }
.shield {
  position: fixed; inset: 0; z-index: 1000; display: grid; place-items: center;
  background: var(--bg); text-align: center; padding: 16px;
}
.shield-box p { margin: 6px 0; }
body.shielded dialog::backdrop { visibility: hidden; }
a.icon-btn { display: inline-grid; place-items: center; text-decoration: none; color: var(--primary); font-weight: 700; }

/* Cestino */
.trash-wrap { max-width: 760px; margin: 0 auto; padding: 0 12px 96px; text-align: center; }
.list { padding-bottom: 8px; }
.small-btn { padding: 7px 10px; font-size: .85rem; }

/* Allegati */
.attach { display: flex; flex-direction: column; gap: 6px; }
.attach-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.attach-title { font-size: .9rem; color: var(--muted); }
.attach p { margin: 0; }
.att-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.att { display: flex; align-items: center; gap: 8px; background: var(--surface-2); border-radius: 10px; padding: 4px 4px 4px 10px; }
.field .att { background: var(--surface); }
.att-name { flex: 1; min-width: 0; overflow-wrap: anywhere; font-size: .92rem; }
