/* ==========================================================================
   Second Brain – stile dell'applicazione
   Tema scuro predefinito, tema chiaro opzionale. Mobile first.
   ========================================================================== */

:root {
  --bg: #06070b;
  --bg-elev: #0d0f15;
  --surface: #12141c;
  --surface-2: #181b25;
  --surface-3: #20242f;
  --border: rgba(255, 255, 255, 0.07);
  --border-strong: rgba(255, 255, 255, 0.12);
  --text: #f3f4f7;
  --text-2: #b4b8c4;
  --muted: #7d8291;
  --primary: #3b82f6;
  --primary-2: #6366f1;
  --primary-soft: rgba(59, 130, 246, 0.14);
  --grad: linear-gradient(135deg, #3b82f6 0%, #4f6cf7 50%, #6366f1 100%);
  --danger: #ef4444;
  --danger-soft: rgba(239, 68, 68, 0.14);
  --success: #22c55e;
  --success-soft: rgba(34, 197, 94, 0.14);
  --warning: #f59e0b;
  --warning-soft: rgba(245, 158, 11, 0.15);
  --purple: #8b5cf6;
  --purple-soft: rgba(139, 92, 246, 0.16);
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
  --shadow-sm: 0 2px 10px rgba(0, 0, 0, 0.25);
  --radius: 18px;
  --radius-sm: 12px;
  --radius-xs: 9px;
  --tabbar-h: 62px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Pro Display", "Inter", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --ease: cubic-bezier(0.2, 0.8, 0.2, 1);
  color-scheme: dark;
}

[data-theme="light"] {
  --bg: #f4f5f9;
  --bg-elev: #ffffff;
  --surface: #ffffff;
  --surface-2: #f3f4f8;
  --surface-3: #e8eaf0;
  --border: rgba(15, 23, 42, 0.08);
  --border-strong: rgba(15, 23, 42, 0.14);
  --text: #0f172a;
  --text-2: #3f4659;
  --muted: #7a8193;
  --primary-soft: rgba(59, 130, 246, 0.1);
  --shadow: 0 10px 30px rgba(15, 23, 42, 0.1);
  --shadow-sm: 0 2px 10px rgba(15, 23, 42, 0.06);
  color-scheme: light;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
html, body { margin: 0; padding: 0; background: var(--bg); color: var(--text); font-family: var(--font); font-size: 16px; line-height: 1.45; }
body { min-height: 100vh; min-height: 100dvh; overscroll-behavior-y: none; -webkit-font-smoothing: antialiased; }
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; padding: 0; }
a { color: var(--primary); text-decoration: none; }
img { max-width: 100%; }
svg.i { width: 20px; height: 20px; flex: none; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.grow { flex: 1; min-width: 0; }
.row { display: flex; align-items: center; gap: 10px; }
.wrap { flex-wrap: wrap; }
.center { text-align: center; }
::selection { background: rgba(59, 130, 246, 0.35); }

/* ---------- Layout ---------- */
.app { min-height: 100vh; min-height: 100dvh; }
.main {
  --pxl: calc(16px + var(--safe-left)); --pxr: calc(16px + var(--safe-right));
  padding: 0 var(--pxr) calc(var(--tabbar-h) + var(--safe-bottom) + 28px) var(--pxl);
  max-width: 860px; margin: 0 auto;
}
.view { animation: viewIn 0.28s var(--ease); }
@keyframes viewIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

/* Barra superiore (stile iOS con titolo grande) */
.topbar {
  position: sticky; top: 0; z-index: 20;
  margin: 0 calc(-1 * var(--pxr)) 0 calc(-1 * var(--pxl));
  padding: calc(var(--safe-top) + 8px) var(--pxr) 8px var(--pxl);
  display: flex; align-items: center; gap: 8px; min-height: calc(var(--safe-top) + 52px);
  background: transparent; transition: background 0.2s, border-color 0.2s;
  border-bottom: 1px solid transparent;
}
.topbar.scrolled { background: color-mix(in srgb, var(--bg) 78%, transparent); -webkit-backdrop-filter: saturate(180%) blur(20px); backdrop-filter: saturate(180%) blur(20px); border-bottom-color: var(--border); }
.topbar .tb-title { flex: 1; text-align: center; font-weight: 600; font-size: 16px; opacity: 0; transition: opacity 0.2s; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar.scrolled .tb-title, .topbar.always .tb-title { opacity: 1; }
.topbar .tb-side { display: flex; align-items: center; gap: 4px; min-width: 44px; }
.topbar .tb-side.right { justify-content: flex-end; }
.icon-btn { width: 40px; height: 40px; display: inline-grid; place-items: center; border-radius: 12px; color: var(--text); transition: background 0.15s, transform 0.1s; }
.icon-btn:active { transform: scale(0.92); background: var(--surface-2); }
.icon-btn.soft { background: var(--surface); border: 1px solid var(--border); }
.icon-btn.active { color: var(--warning); }
.text-btn { color: var(--primary); font-weight: 500; padding: 8px 6px; border-radius: 10px; }
.text-btn:active { opacity: 0.6; }

.page-head { padding: 2px 0 14px; }
.page-title { font-size: 32px; line-height: 1.12; font-weight: 750; letter-spacing: -0.02em; margin: 0; display: flex; align-items: center; gap: 10px; }
.page-title .ti { width: 38px; height: 38px; border-radius: 12px; display: grid; place-items: center; }
.page-sub { color: var(--text-2); margin: 6px 0 0; font-size: 15px; }

/* ---------- Tab bar (iPhone) ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 40;
  display: grid; grid-template-columns: repeat(5, 1fr);
  padding: 6px calc(8px + var(--safe-right)) calc(var(--safe-bottom) + 4px) calc(8px + var(--safe-left));
  background: color-mix(in srgb, var(--bg-elev) 82%, transparent);
  -webkit-backdrop-filter: saturate(180%) blur(22px); backdrop-filter: saturate(180%) blur(22px);
  border-top: 1px solid var(--border);
}
.tab { display: flex; flex-direction: column; align-items: center; gap: 3px; padding: 6px 0 4px; color: var(--muted); font-size: 10.5px; font-weight: 500; border-radius: 12px; transition: color 0.15s; }
.tab .i { width: 23px; height: 23px; }
.tab.active { color: var(--primary); }
.tab:active { transform: scale(0.94); }

/* ---------- Sidebar (desktop/tablet) ---------- */
.sidebar { display: none; }
@media (min-width: 900px) {
  .tabbar { display: none; }
  .sidebar {
    display: flex; flex-direction: column; position: fixed; top: 0; bottom: 0; left: 0; width: 260px; z-index: 30;
    padding: calc(var(--safe-top) + 22px) 14px 18px; background: var(--bg-elev); border-right: 1px solid var(--border); overflow-y: auto;
  }
  .main { margin-left: 260px; max-width: none; --pxl: max(40px, calc((100vw - 260px - 900px) / 2)); --pxr: var(--pxl); padding-bottom: 60px; }
  .chips, .hscroll { margin: 0; padding-left: 0; padding-right: 0; }
  .fab { bottom: 32px !important; right: 40px !important; }
  .toast-wrap { bottom: 28px !important; }
}
.brand { display: flex; align-items: center; gap: 10px; padding: 0 10px 22px; font-weight: 700; font-size: 17px; }
.brand img { width: 34px; height: 34px; border-radius: 10px; }
.side-group { font-size: 11.5px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); padding: 16px 12px 6px; font-weight: 600; }
.side-link { display: flex; align-items: center; gap: 12px; padding: 9px 12px; border-radius: 11px; color: var(--text-2); font-weight: 500; font-size: 14.5px; }
.side-link:hover { background: var(--surface); color: var(--text); }
.side-link.active { background: var(--primary-soft); color: var(--primary); }
.side-link .count { margin-left: auto; font-size: 12px; color: var(--muted); }
.side-foot { margin-top: auto; padding: 14px 10px 0; }

/* ---------- Componenti base ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; }
.card + .card { margin-top: 10px; }
.tiles > .card + .card, .tiles > .tile + .tile { margin-top: 0; }
.section { margin-top: 26px; }
.section-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.section-title { font-size: 19px; font-weight: 700; letter-spacing: -0.01em; margin: 0; }
.section-link { color: var(--primary); font-size: 14px; font-weight: 500; display: inline-flex; align-items: center; gap: 2px; }
.section-link .i { width: 16px; height: 16px; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; height: 48px; padding: 0 18px; border-radius: 14px; font-weight: 600; font-size: 15.5px; transition: transform 0.12s var(--ease), opacity 0.15s, box-shadow 0.2s; user-select: none; }
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.5; pointer-events: none; }
.btn-primary { background: var(--grad); color: #fff; box-shadow: 0 8px 24px rgba(59, 130, 246, 0.3); }
.btn-secondary { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); }
.btn-danger { background: var(--danger-soft); color: var(--danger); }
.btn-ghost { color: var(--primary); }
.btn-block { width: 100%; }
.btn-sm { height: 36px; padding: 0 12px; font-size: 14px; border-radius: 11px; }

.input, .textarea, .select {
  width: 100%; background: var(--surface); border: 1px solid var(--border); border-radius: 13px; padding: 12px 14px; outline: none;
  transition: border-color 0.15s, box-shadow 0.15s; font-size: 16px;
}
.input:focus, .textarea:focus, .select:focus { border-color: var(--primary); box-shadow: 0 0 0 4px var(--primary-soft); }
.textarea { resize: vertical; min-height: 96px; line-height: 1.5; }
.select { appearance: none; -webkit-appearance: none; background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%237d8291' stroke-width='2.5'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E"); background-repeat: no-repeat; background-position: right 14px center; padding-right: 36px; }
.label { display: block; font-size: 13px; font-weight: 600; color: var(--text-2); margin: 0 0 6px 2px; }
.field { margin-bottom: 14px; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.search { position: relative; }
.search .i { position: absolute; left: 14px; top: 50%; transform: translateY(-50%); color: var(--muted); width: 18px; height: 18px; pointer-events: none; }
.search .input { padding-left: 42px; background: var(--surface); border-radius: 14px; height: 46px; }
.search-btn { display: flex; align-items: center; gap: 10px; width: 100%; height: 46px; padding: 0 14px; border-radius: 14px; background: var(--surface); border: 1px solid var(--border); color: var(--muted); text-align: left; }

/* Segmented control */
.segmented { display: flex; background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 4px; gap: 4px; overflow-x: auto; scrollbar-width: none; }
.segmented::-webkit-scrollbar { display: none; }
.segmented button { flex: 1; white-space: nowrap; padding: 8px 12px; border-radius: 10px; font-weight: 600; font-size: 14px; color: var(--text-2); transition: background 0.2s, color 0.2s; }
.segmented button.active { background: var(--surface-3); color: var(--text); box-shadow: var(--shadow-sm); }
[data-theme="light"] .segmented button.active { background: #fff; }

/* Chips */
.chips { display: flex; gap: 8px; overflow-x: auto; padding: 2px 0; scrollbar-width: none; margin: 0 -16px; padding-left: 16px; padding-right: 16px; }
.chips::-webkit-scrollbar { display: none; }
.chip { display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; padding: 8px 14px; border-radius: 999px; background: var(--surface); border: 1px solid var(--border); font-size: 14px; font-weight: 500; color: var(--text-2); transition: all 0.15s; }
.chip.active { background: var(--primary); border-color: var(--primary); color: #fff; }
.chip .i { width: 15px; height: 15px; }
.chip .x { opacity: 0.7; margin-left: 2px; }
.chip.sm { padding: 5px 10px; font-size: 13px; }
.dot { width: 9px; height: 9px; border-radius: 50%; display: inline-block; flex: none; }

.badge { display: inline-flex; align-items: center; gap: 4px; padding: 2px 8px; border-radius: 7px; font-size: 11.5px; font-weight: 600; white-space: nowrap; }
.b-blue { background: var(--primary-soft); color: #60a5fa; }
.b-green { background: var(--success-soft); color: #4ade80; }
.b-amber { background: var(--warning-soft); color: #fbbf24; }
.b-red { background: var(--danger-soft); color: #f87171; }
.b-purple { background: var(--purple-soft); color: #a78bfa; }
.b-gray { background: var(--surface-3); color: var(--text-2); }
[data-theme="light"] .b-blue { color: #2563eb; }
[data-theme="light"] .b-green { color: #16a34a; }
[data-theme="light"] .b-amber { color: #b45309; }
[data-theme="light"] .b-red { color: #dc2626; }
[data-theme="light"] .b-purple { color: #7c3aed; }
.prio { font-size: 12px; font-weight: 600; display: inline-flex; align-items: center; gap: 3px; }
.prio-alta { color: #f97316; } .prio-media { color: #f59e0b; } .prio-bassa { color: #22c55e; }

/* ---------- Statistiche rapide ---------- */
.tiles { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
@media (min-width: 640px) { .tiles.four { grid-template-columns: repeat(4, 1fr); } }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; display: flex; align-items: center; gap: 12px; text-align: left; transition: transform 0.12s; }
.tile:active { transform: scale(0.97); }
.tile .ti { width: 42px; height: 42px; border-radius: 13px; display: grid; place-items: center; flex: none; }
.tile .ti .i { width: 22px; height: 22px; }
.tile .num { font-size: 24px; font-weight: 750; line-height: 1; letter-spacing: -0.02em; }
.tile .lbl { font-size: 13px; color: var(--text-2); margin-top: 3px; }
.ti-blue { background: rgba(59, 130, 246, 0.16); color: #60a5fa; }
.ti-amber { background: rgba(245, 158, 11, 0.16); color: #fbbf24; }
.ti-purple { background: rgba(139, 92, 246, 0.18); color: #a78bfa; }
.ti-green { background: rgba(34, 197, 94, 0.16); color: #4ade80; }
.ti-red { background: rgba(239, 68, 68, 0.16); color: #f87171; }
.ti-pink { background: rgba(236, 72, 153, 0.16); color: #f472b6; }
.ti-gray { background: var(--surface-3); color: var(--text-2); }

/* ---------- Liste ed elementi ---------- */
.list { display: flex; flex-direction: column; gap: 10px; }
.item-card {
  position: relative; display: flex; gap: 12px; align-items: center; padding: 12px; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); text-align: left; width: 100%; transition: transform 0.15s var(--ease), background 0.15s; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
}
.item-card:active { transform: scale(0.985); background: var(--surface-2); }
.item-card .thumb { width: 58px; height: 58px; border-radius: 13px; flex: none; overflow: hidden; display: grid; place-items: center; background: var(--surface-2); }
.item-card .thumb img { width: 100%; height: 100%; object-fit: cover; }
.item-card .thumb .i { width: 24px; height: 24px; }
.item-card .thumb.book { width: 52px; height: 74px; border-radius: 7px; }
.item-card .body { flex: 1; min-width: 0; }
.item-card .title { font-weight: 600; font-size: 15.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item-card .snippet { color: var(--text-2); font-size: 13.5px; margin-top: 2px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item-card .meta { display: flex; align-items: center; gap: 6px; margin-top: 6px; font-size: 12.5px; color: var(--muted); flex-wrap: wrap; }
.item-card .flags { position: absolute; top: 10px; right: 12px; display: flex; gap: 4px; color: var(--warning); }
.item-card .flags .i { width: 15px; height: 15px; }
.item-card .flags .pin { color: var(--primary); }
.item-card .flags .lock { color: var(--muted); }
.item-card .chev { color: var(--muted); }
.progress { height: 5px; border-radius: 4px; background: var(--surface-3); overflow: hidden; margin-top: 8px; }
.progress > span { display: block; height: 100%; background: var(--success); border-radius: 4px; }
.stars { display: inline-flex; gap: 1px; color: #fbbf24; }
.stars .i { width: 14px; height: 14px; fill: currentColor; }
.stars .off { color: var(--surface-3); }

.hscroll { display: flex; gap: 10px; overflow-x: auto; margin: 0 -16px; padding: 2px 16px 6px; scroll-snap-type: x mandatory; scrollbar-width: none; }
.hscroll::-webkit-scrollbar { display: none; }
.mini-card { flex: none; width: 168px; scroll-snap-align: start; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px; text-align: left; }
.mini-card .ti { width: 34px; height: 34px; border-radius: 11px; display: grid; place-items: center; margin-bottom: 10px; }
.mini-card .ti .i { width: 18px; height: 18px; }
.mini-card .t { font-weight: 600; font-size: 14.5px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; min-height: 2.6em; }
.mini-card .m { color: var(--muted); font-size: 12px; margin-top: 6px; }

.row-link { display: flex; align-items: center; gap: 12px; padding: 13px 14px; width: 100%; text-align: left; background: var(--surface); border-bottom: 1px solid var(--border); }
.row-link:active { background: var(--surface-2); }
.group { border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; background: var(--surface); }
.group .row-link:last-child { border-bottom: 0; }
.row-link .ti { width: 32px; height: 32px; border-radius: 9px; display: grid; place-items: center; flex: none; }
.row-link .ti .i { width: 18px; height: 18px; }
.row-link .lbl { flex: 1; font-weight: 500; min-width: 0; }
.row-link .val { color: var(--muted); font-size: 14px; }
.row-link .chev { color: var(--muted); width: 18px; height: 18px; }
.row-link.danger { color: var(--danger); }

.empty { text-align: center; padding: 40px 20px; color: var(--muted); }
.empty .ei { width: 64px; height: 64px; border-radius: 20px; display: grid; place-items: center; margin: 0 auto 14px; background: var(--surface); border: 1px solid var(--border); }
.empty .ei .i { width: 28px; height: 28px; color: var(--text-2); }
.empty h3 { color: var(--text); margin: 0 0 6px; font-size: 17px; }
.empty p { margin: 0 auto 16px; max-width: 320px; font-size: 14.5px; }

/* Continua da dove eri rimasto */
.resume { display: flex; align-items: center; gap: 12px; width: 100%; text-align: left; }

/* Cattura rapida */
.quick { background: linear-gradient(160deg, rgba(59,130,246,0.12), rgba(99,102,241,0.06) 60%, transparent), var(--surface); border: 1px solid rgba(99,102,241,0.25); }
.quick textarea { background: transparent; border: 0; outline: none; width: 100%; resize: none; min-height: 52px; font-size: 16px; line-height: 1.45; padding: 4px 0; }
.quick-foot { display: flex; align-items: center; justify-content: space-between; margin-top: 6px; }

/* FAB */
.fab {
  position: fixed; right: calc(18px + var(--safe-right)); bottom: calc(var(--tabbar-h) + var(--safe-bottom) + 16px); z-index: 35;
  width: 56px; height: 56px; border-radius: 50%; background: var(--grad); color: #fff; display: grid; place-items: center;
  box-shadow: 0 10px 28px rgba(59, 130, 246, 0.45); transition: transform 0.15s var(--ease);
}
.fab:active { transform: scale(0.9); }
.fab .i { width: 26px; height: 26px; stroke-width: 2.4; }

/* ---------- Editor ---------- */
.editor-page .topbar { gap: 4px; }
.save-state { font-size: 12.5px; color: var(--muted); display: inline-flex; align-items: center; gap: 4px; }
.title-input { width: 100%; border: 0; outline: none; background: transparent; font-size: 26px; font-weight: 750; letter-spacing: -0.015em; padding: 6px 0; line-height: 1.2; resize: none; overflow: hidden; }
.title-input::placeholder { color: var(--muted); opacity: 0.7; }
.meta-row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin: 8px 0 12px; }
.toolbar {
  position: sticky; top: calc(var(--safe-top) + 52px); z-index: 15; display: flex; gap: 2px; overflow-x: auto; scrollbar-width: none;
  background: color-mix(in srgb, var(--surface) 92%, transparent); -webkit-backdrop-filter: blur(16px); backdrop-filter: blur(16px);
  border: 1px solid var(--border); border-radius: 14px; padding: 4px; margin-bottom: 12px;
}
.toolbar::-webkit-scrollbar { display: none; }
.toolbar button { flex: none; width: 40px; height: 38px; display: grid; place-items: center; border-radius: 10px; color: var(--text-2); font-weight: 700; font-size: 14px; }
.toolbar button.on { background: var(--primary-soft); color: var(--primary); }
.toolbar button:active { background: var(--surface-3); }
.toolbar .sep { width: 1px; background: var(--border); margin: 6px 3px; flex: none; }

.rich { outline: none; min-height: 40vh; font-size: 17px; line-height: 1.6; word-wrap: break-word; padding-bottom: 40px; }
.rich.compact { min-height: 120px; padding: 12px 14px; background: var(--surface); border: 1px solid var(--border); border-radius: 13px; font-size: 16px; }
.rich:empty::before { content: attr(data-placeholder); color: var(--muted); pointer-events: none; }
.prose h1, .rich h1 { font-size: 1.55em; line-height: 1.25; margin: 0.8em 0 0.35em; letter-spacing: -0.01em; }
.prose h2, .rich h2 { font-size: 1.3em; line-height: 1.3; margin: 0.8em 0 0.35em; }
.prose h3, .rich h3 { font-size: 1.1em; margin: 0.8em 0 0.3em; }
.prose p, .rich p { margin: 0.35em 0; }
.prose blockquote, .rich blockquote { margin: 0.8em 0; padding: 12px 16px; border-left: 3px solid var(--primary); background: var(--primary-soft); border-radius: 0 12px 12px 0; color: var(--text); }
.prose pre, .rich pre { background: var(--surface-2); border: 1px solid var(--border); border-radius: 12px; padding: 12px 14px; font-family: var(--mono); font-size: 13.5px; line-height: 1.5; overflow-x: auto; white-space: pre-wrap; }
.prose code, .rich code { font-family: var(--mono); font-size: 0.9em; background: var(--surface-2); padding: 1px 5px; border-radius: 5px; }
.prose pre code, .rich pre code { background: none; padding: 0; }
.prose img, .rich img { display: block; max-width: 100%; height: auto; border-radius: 14px; margin: 10px 0; }
.prose a, .rich a { color: var(--primary); text-decoration: underline; text-underline-offset: 2px; }
.prose ul, .prose ol, .rich ul, .rich ol { padding-left: 1.4em; margin: 0.4em 0; }
.rich ul.checklist, .prose ul.checklist { list-style: none; padding-left: 0.2em; }
.rich ul.checklist li, .prose ul.checklist li { position: relative; padding-left: 32px; margin: 6px 0; }
.rich ul.checklist li::before, .prose ul.checklist li::before {
  content: ""; position: absolute; left: 0; top: 0.2em; width: 20px; height: 20px; border-radius: 6px; border: 2px solid var(--muted); box-sizing: border-box; cursor: pointer;
}
.rich ul.checklist li[data-checked="true"]::before, .prose ul.checklist li[data-checked="true"]::before {
  background: var(--primary) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='white' stroke-width='3.2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M5 12.5l4.5 4.5L19 7.5'/%3E%3C/svg%3E") center/14px no-repeat; border-color: var(--primary);
}
.rich ul.checklist li[data-checked="true"], .prose ul.checklist li[data-checked="true"] { color: var(--muted); text-decoration: line-through; }
.rich hr, .prose hr { border: 0; border-top: 1px solid var(--border-strong); margin: 1.2em 0; }

.attach-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 8px; }
.attach { position: relative; border-radius: 12px; overflow: hidden; background: var(--surface); border: 1px solid var(--border); aspect-ratio: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 8px; text-align: center; font-size: 11.5px; color: var(--text-2); }
.attach img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.attach .i { width: 26px; height: 26px; margin-bottom: 6px; }
.attach .nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 100%; }
.attach .rm { position: absolute; top: 4px; right: 4px; width: 26px; height: 26px; border-radius: 50%; background: rgba(0,0,0,0.6); color: #fff; display: grid; place-items: center; z-index: 2; }
.attach .rm .i { width: 14px; height: 14px; margin: 0; }
.add-attach { display: flex; align-items: center; justify-content: center; gap: 8px; width: 100%; padding: 14px; border-radius: 14px; border: 1.5px dashed var(--border-strong); color: var(--text-2); font-weight: 500; margin-top: 10px; }
.add-attach:active { background: var(--surface); }
.upload-progress { font-size: 13px; color: var(--muted); margin-top: 8px; }

.cover-pick { width: 110px; height: 160px; border-radius: 10px; overflow: hidden; background: var(--surface); border: 1px dashed var(--border-strong); display: grid; place-items: center; color: var(--muted); flex: none; }
.cover-pick img { width: 100%; height: 100%; object-fit: cover; }
.rating-input { display: flex; gap: 6px; }
.rating-input button .i { width: 28px; height: 28px; color: var(--surface-3); fill: currentColor; stroke: none; }
.rating-input button.on .i { color: #fbbf24; }

.quote-card { background: var(--surface); border: 1px solid var(--border); border-left: 3px solid var(--primary); border-radius: 0 14px 14px 0; padding: 12px 14px; margin-bottom: 8px; position: relative; }
.quote-card p { margin: 0; font-style: italic; }
.quote-card .pg { font-size: 12.5px; color: var(--muted); margin-top: 4px; }
.quote-card .rm { position: absolute; top: 8px; right: 8px; color: var(--muted); }

.link-preview { display: flex; gap: 12px; background: var(--surface); border: 1px solid var(--border); border-radius: 16px; overflow: hidden; }
.link-preview img { width: 120px; object-fit: cover; flex: none; }
.link-preview .lp-body { padding: 12px 12px 12px 0; min-width: 0; }
.link-preview.big { flex-direction: column; gap: 0; }
.link-preview.big img { width: 100%; height: 180px; }
.link-preview.big .lp-body { padding: 12px 14px; }

.footer-meta { color: var(--muted); font-size: 12.5px; margin-top: 26px; border-top: 1px solid var(--border); padding-top: 14px; line-height: 1.7; }

/* ---------- Sheet, modali, toast ---------- */
.overlay { position: fixed; inset: 0; z-index: 100; background: rgba(0, 0, 0, 0.5); animation: fadeIn 0.2s; display: flex; align-items: flex-end; justify-content: center; }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
.sheet {
  width: 100%; max-width: 560px; max-height: 88vh; overflow-y: auto; background: var(--bg-elev); border-radius: 22px 22px 0 0; border: 1px solid var(--border);
  padding: 8px 16px calc(16px + var(--safe-bottom)); animation: sheetIn 0.32s var(--ease); -webkit-overflow-scrolling: touch;
}
@keyframes sheetIn { from { transform: translateY(100%); } to { transform: none; } }
.sheet.closing { animation: sheetOut 0.22s ease-in forwards; }
@keyframes sheetOut { to { transform: translateY(100%); } }
.overlay.closing { animation: fadeOut 0.22s forwards; }
@keyframes fadeOut { to { opacity: 0; } }
.grabber { width: 40px; height: 5px; border-radius: 3px; background: var(--surface-3); margin: 4px auto 12px; }
.sheet h3 { margin: 4px 4px 14px; font-size: 18px; }
.sheet .action { display: flex; align-items: center; gap: 12px; width: 100%; padding: 14px 12px; border-radius: 13px; font-size: 16px; font-weight: 500; text-align: left; }
.sheet .action:active { background: var(--surface); }
.sheet .action.danger { color: var(--danger); }
.sheet .action .i { color: var(--text-2); }
.sheet .action.danger .i { color: var(--danger); }
@media (min-width: 700px) {
  .overlay { align-items: center; }
  .sheet { border-radius: 22px; padding-bottom: 18px; animation: popIn 0.25s var(--ease); }
  @keyframes popIn { from { transform: scale(0.95); opacity: 0; } to { transform: none; opacity: 1; } }
}
.color-grid { display: grid; grid-template-columns: repeat(8, 1fr); gap: 8px; }
.color-grid button { aspect-ratio: 1; border-radius: 50%; border: 3px solid transparent; }
.color-grid button.on { border-color: var(--text); }

.toast-wrap { position: fixed; left: 0; right: 0; bottom: calc(var(--tabbar-h) + var(--safe-bottom) + 14px); z-index: 200; display: flex; flex-direction: column; align-items: center; gap: 8px; pointer-events: none; padding: 0 16px; }
.toast { pointer-events: auto; display: flex; align-items: center; gap: 12px; max-width: 520px; background: color-mix(in srgb, var(--surface-3) 94%, transparent); -webkit-backdrop-filter: blur(16px); backdrop-filter: blur(16px); color: var(--text); padding: 12px 16px; border-radius: 14px; box-shadow: var(--shadow); font-size: 14.5px; animation: toastIn 0.3s var(--ease); border: 1px solid var(--border); }
.toast.err { border-color: rgba(239, 68, 68, 0.4); }
.toast button { color: var(--primary); font-weight: 600; white-space: nowrap; }
@keyframes toastIn { from { transform: translateY(16px); opacity: 0; } to { transform: none; opacity: 1; } }

.net-banner { position: fixed; top: calc(var(--safe-top) + 6px); left: 50%; transform: translateX(-50%); z-index: 150; background: var(--surface-3); color: var(--text-2); font-size: 12.5px; font-weight: 600; padding: 6px 12px; border-radius: 999px; display: flex; align-items: center; gap: 6px; box-shadow: var(--shadow-sm); }
.net-banner .i { width: 14px; height: 14px; }

.ptr { position: fixed; top: calc(var(--safe-top) + 8px); left: 50%; transform: translate(-50%, -60px); z-index: 50; width: 38px; height: 38px; border-radius: 50%; background: var(--surface-2); border: 1px solid var(--border); display: grid; place-items: center; box-shadow: var(--shadow-sm); transition: transform 0.2s; }
.ptr .i { width: 18px; height: 18px; color: var(--primary); }
.ptr.spin .i { animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.sync-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--success); display: inline-block; }
.sync-dot.pending { background: var(--warning); }
.sync-dot.offline { background: var(--muted); }
.sync-dot.error { background: var(--danger); }

/* ---------- Calendario ---------- */
.cal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.cal-head h3 { margin: 0; font-size: 17px; text-transform: capitalize; }
.cal { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; }
.cal .dow { text-align: center; font-size: 11.5px; color: var(--muted); font-weight: 600; padding: 4px 0; }
.cal .day { aspect-ratio: 1; border-radius: 11px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px; font-size: 14px; font-weight: 500; position: relative; }
.cal .day.out { opacity: 0.3; }
.cal .day.today { color: var(--primary); font-weight: 700; }
.cal .day.sel { background: var(--primary); color: #fff; }
.cal .day .dots { display: flex; gap: 2px; height: 5px; }
.cal .day .dots span { width: 5px; height: 5px; border-radius: 50%; background: var(--primary); }
.cal .day.sel .dots span { background: #fff; }
.cal .day.l1 { background: rgba(59,130,246,0.08); } .cal .day.l2 { background: rgba(59,130,246,0.16); } .cal .day.l3 { background: rgba(59,130,246,0.26); }
.cal .day.sel.l1, .cal .day.sel.l2, .cal .day.sel.l3 { background: var(--primary); }
.tl-day { font-size: 13px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; margin: 18px 2px 8px; }

/* ---------- Grafici ---------- */
.chart { width: 100%; height: auto; display: block; }
.chart text { fill: var(--muted); font-size: 10px; font-family: var(--font); }
.legend { display: grid; gap: 8px; }
.legend .lg { display: flex; align-items: center; gap: 8px; font-size: 14px; }
.legend .lg .v { margin-left: auto; color: var(--text-2); font-variant-numeric: tabular-nums; }
.donut-wrap { display: flex; align-items: center; gap: 18px; }
.donut-wrap svg { width: 132px; height: 132px; flex: none; }
.big-num { font-size: 30px; font-weight: 750; letter-spacing: -0.02em; }
.hbar { height: 8px; background: var(--surface-3); border-radius: 5px; overflow: hidden; margin-top: 5px; }
.hbar span { display: block; height: 100%; border-radius: 5px; }

/* ---------- Login ---------- */
.auth { min-height: 100vh; min-height: 100dvh; display: grid; place-items: center; padding: calc(var(--safe-top) + 24px) 22px calc(var(--safe-bottom) + 24px);
  background: radial-gradient(1200px 600px at 50% -10%, rgba(59,130,246,0.22), transparent 60%), radial-gradient(800px 500px at 100% 100%, rgba(99,102,241,0.14), transparent 60%), var(--bg); }
.auth-box { width: 100%; max-width: 400px; animation: viewIn 0.4s var(--ease); }
.auth-logo { width: 76px; height: 76px; border-radius: 22px; margin: 0 auto 18px; display: block; box-shadow: 0 16px 40px rgba(59,130,246,0.35); }
.auth h1 { text-align: center; font-size: 28px; margin: 0 0 6px; letter-spacing: -0.02em; }
.auth .sub { text-align: center; color: var(--text-2); margin: 0 0 26px; }
.auth .err { color: var(--danger); font-size: 14px; margin: 4px 2px 12px; min-height: 1em; }
.notice { background: var(--warning-soft); color: var(--text); border-radius: 14px; padding: 12px 14px; font-size: 14px; margin-bottom: 16px; border: 1px solid rgba(245, 158, 11, 0.3); }

/* Evidenziazione ricerca */
mark { background: rgba(250, 204, 21, 0.3); color: inherit; border-radius: 3px; padding: 0 1px; }

/* Stampa / PDF */
#print-root { display: none; }
@media print {
  body.printing > *:not(#print-root) { display: none !important; }
  body.printing #print-root { display: block; color: #000; background: #fff; padding: 0; font-family: Georgia, "Times New Roman", serif; }
  #print-root h1.pt { font-family: var(--font); font-size: 26px; margin: 0 0 4px; }
  #print-root .pm { font-family: var(--font); color: #555; font-size: 12px; margin-bottom: 18px; }
  #print-root .prose blockquote { background: #f3f4f6; color: #000; }
  #print-root .prose pre { background: #f3f4f6; color: #000; border-color: #ddd; }
  html, body { background: #fff !important; }
}

@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation-duration: 0.001ms !important; transition-duration: 0.001ms !important; } }
