:root {
  --bg: #eef1f6;
  --bg-2: #f7f9fc;
  --panel: #ffffff;
  --ink: #0f172a;
  --ink-soft: #334155;
  --muted: #64748b;
  --line: #e6e9f0;
  --line-soft: #eff2f7;
  --subtle: #fbfcfe;
  --primary: #6366f1;
  --primary-d: #4f46e5;
  --primary-2: #8b5cf6;
  --accent: #06b6d4;
  --danger: #ef4444;
  --danger-bg: #fef2f2;
  --ok: #10b981;
  --ok-bg: #ecfdf5;
  --warn: #f59e0b;
  --warn-bg: #fffbeb;
  --radius: 14px;
  --radius-sm: 10px;
  --shadow-sm: 0 1px 2px rgba(15,23,42,.06), 0 1px 3px rgba(15,23,42,.05);
  --shadow: 0 4px 6px -1px rgba(15,23,42,.07), 0 2px 4px -2px rgba(15,23,42,.05);
  --shadow-lg: 0 12px 28px -8px rgba(15,23,42,.18);
  --sidebar-1: #1e1b4b;
  --sidebar-2: #312e81;
  --sidebar-ink: #c7cbe6;
  --font: 'Inter', system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  color: var(--ink);
  background: linear-gradient(180deg, var(--bg-2), var(--bg));
  line-height: 1.55;
  font-size: 14.5px;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--primary-d); text-decoration: none; transition: color .15s; }
a:hover { color: var(--primary); }
h1 { font-size: 1.6rem; margin: 0 0 1.25rem; font-weight: 700; letter-spacing: -.02em; }
h2 { font-size: 1.1rem; margin: 0 0 .85rem; font-weight: 650; letter-spacing: -.01em; }
.mono { font-family: ui-monospace, "SFMono-Regular", Menlo, monospace; }
.muted { color: var(--muted); }

/* ===== Layout ===== */
.layout { display: flex; min-height: 100vh; }
.sidebar {
  width: 244px; flex-shrink: 0;
  background: linear-gradient(165deg, var(--sidebar-2), var(--sidebar-1) 70%);
  color: var(--sidebar-ink);
  display: flex; flex-direction: column; padding: 1.1rem .75rem;
  position: sticky; top: 0; height: 100vh;
}
.brand { font-weight: 800; font-size: 1.25rem; color: #fff; padding: .4rem .6rem 1rem; letter-spacing: -.02em; display: flex; align-items: center; gap: .5rem; }
.brand-icon { display: inline-grid; place-items: center; width: 32px; height: 32px; border-radius: 9px; background: linear-gradient(135deg, var(--primary), var(--primary-2)); box-shadow: 0 4px 12px rgba(99,102,241,.5); }
.sidebar nav { display: flex; flex-direction: column; gap: 2px; flex: 1; margin-top: .4rem; }
.sidebar nav a {
  color: var(--sidebar-ink); padding: .6rem .7rem; font-size: .92rem; font-weight: 500;
  border-radius: var(--radius-sm); display: flex; align-items: center; gap: .7rem;
  transition: background .15s, color .15s, transform .1s;
}
.sidebar nav a .ico { font-size: 1.05rem; width: 20px; text-align: center; filter: saturate(.4) brightness(1.4); }
.sidebar nav a:hover { background: rgba(255,255,255,.07); color: #fff; }
.sidebar nav a.active { background: linear-gradient(135deg, rgba(99,102,241,.9), rgba(139,92,246,.85)); color: #fff; box-shadow: 0 6px 16px -6px rgba(99,102,241,.7); }
.sidebar nav a.active .ico { filter: none; }
.logout { padding: .75rem .3rem 0; border-top: 1px solid rgba(255,255,255,.1); margin-top: .5rem; }
.whoami { display: flex; align-items: center; gap: .55rem; font-size: .85rem; color: #e2e4f3; margin-bottom: .6rem; padding: 0 .3rem; }
.avatar { display: inline-grid; place-items: center; width: 30px; height: 30px; border-radius: 50%; background: linear-gradient(135deg, var(--accent), var(--primary)); color: #fff; font-weight: 700; font-size: .85rem; }
.tenant-chip { background: rgba(255,255,255,.06); border: 1px solid rgba(255,255,255,.08); border-radius: var(--radius-sm); padding: .55rem .7rem; margin: 0 .3rem .3rem; }
.tenant-label { display: block; font-size: .65rem; text-transform: uppercase; letter-spacing: .08em; color: #a5a9d0; }
.tenant-name { color: #fff; font-weight: 650; font-size: .95rem; }
.tenant-switch { display: inline-block; margin-left: .4rem; font-size: .72rem; color: #a5b4fc; }
.content { flex: 1; padding: 2.2rem 2.4rem; max-width: 1080px; width: 100%; }

/* ===== Auth ===== */
.auth-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 1rem;
  background: radial-gradient(1200px 500px at 50% -10%, rgba(99,102,241,.18), transparent), linear-gradient(180deg, var(--bg-2), var(--bg)); }
.login-card { width: 360px; text-align: center; padding: 2rem 1.75rem; }
.brand-lg { font-size: 1.9rem; font-weight: 800; letter-spacing: -.02em; }

/* ===== Cards ===== */
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 1.4rem; box-shadow: var(--shadow-sm); margin-bottom: 1.4rem; }
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 1.1rem; margin-bottom: 1.4rem; }
.stat { position: relative; text-align: left; display: block; overflow: hidden; transition: transform .16s, box-shadow .16s; padding-left: 1.4rem; }
.stat::before { content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 5px; background: linear-gradient(180deg, var(--primary), var(--primary-2)); }
.stat:hover { transform: translateY(-3px); box-shadow: var(--shadow-lg); }
.stat-num { font-size: 2.1rem; font-weight: 800; letter-spacing: -.03em; background: linear-gradient(135deg, var(--primary-d), var(--primary-2)); -webkit-background-clip: text; background-clip: text; -webkit-text-fill-color: transparent; }
.stat-label { color: var(--muted); font-size: .9rem; font-weight: 500; }
.empty { text-align: center; padding: 2.5rem 1.4rem; }

/* ===== Page head ===== */
.page-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 1.4rem; gap: 1rem; flex-wrap: wrap; }
.page-head h1 { margin: 0; }

/* ===== Status / picker ===== */
.status-bar { padding: .7rem 1.1rem; border-radius: var(--radius-sm); margin-bottom: 1.4rem; font-size: .9rem; font-weight: 500; display: flex; align-items: center; gap: .5rem; }
.status-bar.ok { background: var(--ok-bg); color: #047857; border: 1px solid #a7f3d0; }
.status-bar.err { background: var(--danger-bg); color: #b91c1c; border: 1px solid #fecaca; }
.tenant-picker { display: flex; align-items: center; gap: .7rem; }
.tenant-picker label { font-weight: 600; font-size: .9rem; margin: 0; white-space: nowrap; }
.tenant-picker select { flex: 1; }

/* ===== Tables ===== */
.table { width: 100%; border-collapse: separate; border-spacing: 0; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow-sm); }
.table th, .table td { text-align: left; padding: .8rem 1rem; border-bottom: 1px solid var(--line-soft); font-size: .92rem; }
.table th { background: var(--subtle); color: var(--muted); font-weight: 600; font-size: .74rem; text-transform: uppercase; letter-spacing: .05em; }
.table tbody tr { transition: background .12s; }
.table tbody tr:hover { background: var(--subtle); }
.table tr:last-child td { border-bottom: none; }
.actions { display: flex; gap: .4rem; flex-wrap: wrap; }
.actions form { display: inline; }

/* ===== Badges ===== */
.badge { display: inline-flex; align-items: center; padding: .2rem .6rem; border-radius: 999px; font-size: .74rem; font-weight: 600; background: #eef1f6; color: var(--muted); }
.badge.ok { background: var(--ok-bg); color: #047857; }
.badge.warn { background: var(--warn-bg); color: #b45309; }
.badge.err { background: var(--danger-bg); color: #b91c1c; }

/* ===== Forms ===== */
.form label { display: block; margin-bottom: 1.05rem; font-size: .9rem; font-weight: 600; color: var(--ink-soft); }
.form input[type=text], .form input[type=password], .form input[type=number],
.form input[type=time], .form input[type=date], .form input[type=file], .form select {
  width: 100%; margin-top: .4rem; padding: .62rem .7rem; border: 1px solid var(--line);
  border-radius: var(--radius-sm); font-size: .95rem; font-weight: 400; background: var(--panel);
  font-family: inherit; color: var(--ink); transition: border-color .15s, box-shadow .15s;
}
.form input:focus, .form select:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(99,102,241,.15); }
.form small { display: block; font-weight: 400; margin-top: .3rem; color: var(--muted); font-size: .82rem; }
.form .row { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
label.check { display: flex; align-items: center; gap: .55rem; font-weight: 500; color: var(--ink-soft); }
label.check input { width: auto; margin: 0; width: 16px; height: 16px; accent-color: var(--primary); }
.member-select { border: 1px solid var(--line); border-radius: var(--radius-sm); padding: 1rem; margin-bottom: 1.05rem; background: var(--subtle); }
.member-select legend { font-weight: 600; font-size: .82rem; padding: 0 .45rem; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.member-select label.check { padding: .25rem 0; }
.form-actions { margin-top: .5rem; }

/* ===== Buttons ===== */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: .4rem; padding: .55rem 1rem; border-radius: var(--radius-sm); border: 1px solid var(--line); background: var(--panel); color: var(--ink-soft); font-size: .9rem; font-weight: 600; cursor: pointer; font-family: inherit; transition: all .15s; box-shadow: var(--shadow-sm); }
.btn:hover { background: var(--subtle); transform: translateY(-1px); box-shadow: var(--shadow); }
.btn:active { transform: translateY(0); }
.btn-primary { background: linear-gradient(135deg, var(--primary), var(--primary-2)); border-color: transparent; color: #fff; box-shadow: 0 6px 16px -6px rgba(99,102,241,.6); }
.btn-primary:hover { background: linear-gradient(135deg, var(--primary-d), var(--primary-2)); color: #fff; }
.btn-ghost { background: transparent; border-color: transparent; box-shadow: none; color: var(--muted); }
.btn-ghost:hover { background: rgba(148,163,184,.14); box-shadow: none; }
.sidebar .btn-ghost { color: #c7cbe6; }
.sidebar .btn-ghost:hover { background: rgba(255,255,255,.1); color: #fff; }
.btn-danger { color: var(--danger); border-color: #f3c9c9; }
.btn-danger:hover { background: var(--danger-bg); }
.btn-sm { padding: .34rem .7rem; font-size: .82rem; }
.btn-block { width: 100%; }

/* ===== Flash ===== */
.flashes { margin-bottom: 1.1rem; display: flex; flex-direction: column; gap: .5rem; }
.flash { padding: .7rem 1rem; border-radius: var(--radius-sm); font-size: .9rem; font-weight: 500; border: 1px solid transparent; box-shadow: var(--shadow-sm); animation: slidein .25s ease; }
@keyframes slidein { from { opacity: 0; transform: translateY(-6px); } to { opacity: 1; transform: none; } }
.flash-success { background: var(--ok-bg); color: #047857; border-color: #a7f3d0; }
.flash-error { background: var(--danger-bg); color: #b91c1c; border-color: #fecaca; }
.flash-warning { background: var(--warn-bg); color: #b45309; border-color: #fde68a; }
.flash-info { background: #eef2ff; color: var(--primary-d); border-color: #c7d2fe; }

.reg-list { list-style: none; padding: 0; margin: 0; }
.reg-list li { padding: .4rem 0; border-bottom: 1px solid var(--line-soft); font-family: ui-monospace, monospace; font-size: .9rem; }
.reg-list li:last-child { border-bottom: none; }

/* ===== Responsive ===== */
@media (max-width: 820px) {
  .layout { flex-direction: column; }
  .sidebar { width: 100%; height: auto; position: static; flex-direction: column; }
  .sidebar nav { flex-direction: row; flex-wrap: wrap; }
  .sidebar nav a { flex: 1 1 auto; }
  .content { padding: 1.2rem; }
  .form .row { grid-template-columns: 1fr; }
}

/* ============================================================
   TEMI SELEZIONABILI (data-theme su <html>)
   ============================================================ */

/* --- Scuro --- */
:root[data-theme="dark"] {
  --bg: #0b1020;
  --bg-2: #0e1428;
  --panel: #141b2e;
  --ink: #e8ebf5;
  --ink-soft: #c4cadb;
  --muted: #8a93ab;
  --line: #26304a;
  --line-soft: #1c2437;
  --subtle: #101827;
  --sidebar-1: #070b17;
  --sidebar-2: #121a33;
  --sidebar-ink: #c4cadb;
  --shadow-sm: 0 1px 2px rgba(0,0,0,.4);
  --shadow: 0 4px 10px rgba(0,0,0,.45);
  --shadow-lg: 0 14px 34px -10px rgba(0,0,0,.6);
  --danger-bg: #2a1417; --ok-bg: #0e2420; --warn-bg: #2a2110;
}
:root[data-theme="dark"] .badge { background: #1d2740; }
:root[data-theme="dark"] .status-bar.ok { border-color: #14532d; }
:root[data-theme="dark"] .status-bar.err { border-color: #7f1d1d; }
:root[data-theme="dark"] .flash-success { border-color: #14532d; color: #6ee7b7; }
:root[data-theme="dark"] .flash-error { border-color: #7f1d1d; color: #fca5a5; }
:root[data-theme="dark"] .flash-warning { border-color: #78350f; color: #fcd34d; }
:root[data-theme="dark"] .flash-info { background: #172554; border-color: #1e3a8a; color: #93c5fd; }

/* --- Glassy (vetro) --- */
:root[data-theme="glassy"] {
  --panel: rgba(255,255,255,.62);
  --line: rgba(255,255,255,.65);
  --subtle: rgba(255,255,255,.4);
  --ink: #221b4d;
  --ink-soft: #3a338f;
  --muted: #6b62a6;
  --sidebar-1: rgba(49,46,129,.85);
  --sidebar-2: rgba(99,102,241,.75);
}
:root[data-theme="glassy"] body {
  background: linear-gradient(135deg, #a5b4fc, #f0abfc 55%, #7dd3fc);
  background-attachment: fixed;
}
:root[data-theme="glassy"] .sidebar {
  backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
  border-right: 1px solid rgba(255,255,255,.25);
}
:root[data-theme="glassy"] .card,
:root[data-theme="glassy"] .table {
  backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
  border: 1px solid rgba(255,255,255,.55);
  box-shadow: 0 8px 32px rgba(31,38,135,.15);
}
:root[data-theme="glassy"] .table th { background: rgba(255,255,255,.45); }

/* --- Selettore tema nella sidebar --- */
.theme-switch { display: flex; gap: .3rem; padding: .5rem .3rem 0; }
.theme-switch a {
  flex: 1; border: 1px solid rgba(255,255,255,.15); background: rgba(255,255,255,.06);
  border-radius: 8px; padding: .35rem 0; cursor: pointer; font-size: 1rem; text-align: center;
  transition: background .15s, transform .1s;
}
.theme-switch a:hover { background: rgba(255,255,255,.16); transform: translateY(-1px); }
.theme-switch a.active { background: linear-gradient(135deg, var(--primary), var(--primary-2)); border-color: transparent; }

/* ============================================================
   ICONE A LINEA (inline SVG, stile Linear/Lucide)
   ============================================================ */
svg.ic { flex-shrink: 0; vertical-align: middle; }
.brand-icon svg { color: #fff; }
.sidebar nav a .ico { display: inline-flex; align-items: center; justify-content: center;
  width: 20px; height: 20px; filter: none; opacity: .85; }
.sidebar nav a .ico svg { width: 18px; height: 18px; }
.sidebar nav a.active .ico { filter: none; opacity: 1; }
.btn svg.ic { width: 16px; height: 16px; }
.theme-switch a { color: var(--sidebar-ink); display: flex; align-items: center; justify-content: center; }
.theme-switch a.active { color: #fff; }

/* ============================================================
   TEMA LINEAR (scuro, sobrio) — predefinito
   ============================================================ */
:root[data-theme="linear"] {
  --bg: #08090a;
  --bg-2: #0b0c0d;
  --panel: #101113;
  --ink: #f7f8f8;
  --ink-soft: #d0d3d8;
  --muted: #8a8f98;
  --line: #23252a;
  --line-soft: #191b1f;
  --subtle: #141518;
  --primary: #858ff0;
  --primary-d: #5e6ad2;
  --primary-2: #8f7cf0;
  --accent: #4cb7c7;
  --danger: #eb5757; --danger-bg: #26161a;
  --ok: #4cb782; --ok-bg: #10231c;
  --warn: #f2c94c; --warn-bg: #241f10;
  --radius: 12px; --radius-sm: 8px;
  --shadow-sm: 0 1px 2px rgba(0,0,0,.5);
  --shadow: 0 4px 12px rgba(0,0,0,.5);
  --shadow-lg: 0 16px 40px -12px rgba(0,0,0,.7);
  --sidebar-1: #0a0b0c;
  --sidebar-2: #0a0b0c;
  --sidebar-ink: #b4b8bf;
}
:root[data-theme="linear"] body { background: var(--bg); }
:root[data-theme="linear"] .sidebar { border-right: 1px solid var(--line); }
:root[data-theme="linear"] .brand-icon { background: #191a1e; border: 1px solid var(--line); box-shadow: none; }
:root[data-theme="linear"] .sidebar nav a { color: var(--sidebar-ink); font-weight: 500; }
:root[data-theme="linear"] .sidebar nav a:hover { background: rgba(255,255,255,.045); color: #fff; transform: none; }
:root[data-theme="linear"] .sidebar nav a.active { background: rgba(255,255,255,.07); color: #fff; box-shadow: none; }
:root[data-theme="linear"] .tenant-chip { background: rgba(255,255,255,.04); border-color: var(--line); }
:root[data-theme="linear"] .tenant-label { color: var(--muted); }
:root[data-theme="linear"] .btn-primary { background: var(--primary-d); box-shadow: none; }
:root[data-theme="linear"] .btn-primary:hover { background: var(--primary); }
:root[data-theme="linear"] .stat::before { background: linear-gradient(180deg, var(--primary-d), var(--primary-2)); }
:root[data-theme="linear"] .badge { background: #1b1d22; color: var(--muted); }
:root[data-theme="linear"] .avatar { background: linear-gradient(135deg, var(--accent), var(--primary-d)); }
:root[data-theme="linear"] .status-bar.ok { border-color: #14532d; }
:root[data-theme="linear"] .status-bar.err { border-color: #7f1d1d; }
:root[data-theme="linear"] .flash-success { border-color: #14532d; color: #6ee7b7; }
:root[data-theme="linear"] .flash-error { border-color: #7f1d1d; color: #fca5a5; }
:root[data-theme="linear"] .flash-warning { border-color: #78350f; color: #fcd34d; }
:root[data-theme="linear"] .flash-info { background: #171a2e; border-color: #2a2f52; color: #a5b0f0; }
:root[data-theme="linear"] .theme-switch a { border-color: var(--line); background: rgba(255,255,255,.03); }
:root[data-theme="linear"] .theme-switch a.active { background: var(--primary-d); border-color: transparent; }

/* ============================================================
   EDITOR TASTI BLF (form estensione)
   ============================================================ */
.blf-editor { margin-top: .3rem; }
.blf-rows { display: flex; flex-direction: column; gap: .5rem; margin-bottom: .6rem; }
.blf-row { display: grid; grid-template-columns: 190px 1fr 1fr auto; gap: .5rem; align-items: center; }
.blf-row select, .blf-row input { margin-top: 0; }
.blf-row .blf-del { padding: .45rem .65rem; color: var(--danger); border-color: transparent; box-shadow: none; }
.blf-row .blf-del:hover { background: var(--danger-bg); }
.blf-help { margin-top: .7rem; }
.blf-help summary { cursor: pointer; font-size: .85rem; }
.blf-help ul { margin: .5rem 0 0; padding-left: 1.1rem; font-size: .82rem; line-height: 1.95; }
.blf-help code { background: var(--subtle); border: 1px solid var(--line); border-radius: 5px; padding: .04rem .35rem; font-size: .8rem; }
@media (max-width: 680px) { .blf-row { grid-template-columns: 1fr; } }
