:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --text: #1c2430;
  --muted: #64707d;
  --border: #dfe4ea;
  --primary: #0f766e;
  --primary-text: #ffffff;
  --expired: #c62828;
  --expired-bg: #fdecec;
  --soon: #b7791f;
  --soon-bg: #fff6dc;
  --ok: #2e7d32;
  --radius: 12px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11161c;
    --surface: #1b232c;
    --text: #e6ebf0;
    --muted: #93a0ad;
    --border: #2c3742;
    --primary: #2bb3a3;
    --primary-text: #062b27;
    --expired: #ff6b6b;
    --expired-bg: #3a1c1f;
    --soon: #f0b429;
    --soon-bg: #3a2f14;
    --ok: #66bb6a;
  }
}

* { box-sizing: border-box; }
[x-cloak] { display: none !important; }

html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

.app { max-width: 1200px; margin: 0 auto; padding-bottom: 96px; }

/* Barra in alto */
.topbar {
  position: sticky; top: 0; z-index: 5;
  background: var(--bg);
  padding: calc(12px + env(safe-area-inset-top)) 16px 8px;
  border-bottom: 1px solid var(--border);
}
.topbar-row { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.topbar-row:last-child { margin-bottom: 0; }
.topbar h1 { font-size: 1.25rem; margin: 0; flex: 1; }
.filters { flex-wrap: wrap; font-size: .9rem; }
.filters .toggle { margin-right: auto; }

/* Controlli */
input, select, textarea {
  width: 100%;
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  min-height: 44px;
}
input[type="checkbox"] { width: 20px; height: 20px; min-height: 0; accent-color: var(--primary); margin: 0; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: -1px; }

button { font: inherit; cursor: pointer; border-radius: 8px; border: 0; min-height: 44px; padding: 0 16px; }
button:disabled { opacity: .6; cursor: default; }
.primary { background: var(--primary); color: var(--primary-text); font-weight: 600; }
.secondary { background: transparent; color: var(--text); border: 1px solid var(--border); }
.link { background: none; color: var(--primary); padding: 0 4px; min-height: 32px; }

.toggle { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; }

.pill { font-size: .8rem; font-weight: 600; padding: 2px 10px; border-radius: 99px; }
.pill.expired { background: var(--expired-bg); color: var(--expired); }
.pill.soon { background: var(--soon-bg); color: var(--soon); }

/* Lista */
main { padding: 12px 16px; }
.notice { color: var(--muted); text-align: center; margin: 24px 0; }
.notice.error { color: var(--expired); margin: 8px 0; }
/* Griglia: 1 colonna su telefono, 2 su tablet/finestre medie, 3 su PC */
.list { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; grid-template-columns: 1fr; }
.list > li { display: flex; }
@media (min-width: 700px) { .list { grid-template-columns: repeat(2, 1fr); } }
@media (min-width: 1050px) { .list { grid-template-columns: repeat(3, 1fr); } }

.card {
  display: block; width: 100%; text-align: left;
  align-self: stretch;
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border);
  border-left: 6px solid var(--ok);
  border-radius: var(--radius);
  padding: 12px 14px;
}
.card.none { border-left-color: var(--border); }
.card.soon { border-left-color: var(--soon); background: var(--soon-bg); }
.card.expired { border-left-color: var(--expired); background: var(--expired-bg); }
.card-head { display: flex; align-items: baseline; gap: 8px; }
.card .name { font-weight: 600; font-size: 1.05rem; }
.card .dose { color: var(--muted); font-size: .9rem; }
.card .sub { color: var(--muted); font-size: .9rem; margin-top: 2px; }
.card .expiry { margin-top: 6px; font-size: .95rem; font-weight: 500; }
.card.expired .expiry { color: var(--expired); font-weight: 700; }
.card.soon .expiry { color: var(--soon); font-weight: 700; }
.tags { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.tags:empty { display: none; }
.tag { font-size: .8rem; background: var(--bg); border: 1px solid var(--border); border-radius: 99px; padding: 2px 8px; }
.tag.muted { color: var(--muted); }

.fab {
  position: fixed;
  right: max(16px, calc(50% - 600px + 16px));
  bottom: calc(20px + env(safe-area-inset-bottom));
  width: 60px; height: 60px; border-radius: 50%;
  background: var(--primary); color: var(--primary-text);
  font-size: 2rem; line-height: 1; padding: 0;
  box-shadow: 0 6px 18px rgba(0, 0, 0, .25);
}

/* Form e finestre */
.overlay {
  position: fixed; inset: 0; z-index: 20;
  background: rgba(0, 0, 0, .45);
  display: grid; place-items: center; padding: 16px;
}
.sheet {
  background: var(--surface); color: var(--text);
  border: 0; border-radius: var(--radius);
  padding: 16px;
  width: 100%; max-width: 560px;
}
.sheet.small { max-width: 400px; }
dialog.sheet { max-height: 92vh; overflow-y: auto; }
dialog::backdrop { background: rgba(0, 0, 0, .45); }

/* Su telefono il form occupa tutto lo schermo */
@media (max-width: 600px) {
  dialog.sheet {
    margin: 0; max-width: none; width: 100%; max-height: none; height: 100%;
    border-radius: 0;
    padding: calc(16px + env(safe-area-inset-top)) 16px calc(16px + env(safe-area-inset-bottom));
  }
}

.sheet h2 { margin: 0 0 12px; font-size: 1.2rem; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; }
.sheet-head h2 { margin: 0; }
.sheet form > label, .sheet fieldset, .sheet .grid2, .sheet fieldset > label { margin-top: 12px; }
.sheet fieldset > label:first-of-type { margin-top: 4px; }
.sheet label { display: block; font-size: .85rem; color: var(--muted); }
.sheet label > input, .sheet label > select, .sheet label > textarea { margin-top: 4px; }
.sheet .toggle { font-size: 1rem; color: var(--text); }
.sheet fieldset { border: 1px solid var(--border); border-radius: 8px; padding: 4px 12px 12px; }
.sheet legend { font-size: .85rem; font-weight: 600; padding: 0 4px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.grid2 > label { margin: 0; }
.hint, .meta { color: var(--muted); font-size: .85rem; }
.meta { margin-top: 16px; }
.actions { display: flex; gap: 8px; margin-top: 16px; flex-wrap: wrap; }
.actions .spacer { flex: 1; }

/* Notifiche */
.toast {
  position: fixed; z-index: 30;
  left: 50%; transform: translateX(-50%);
  bottom: calc(96px + env(safe-area-inset-bottom));
  max-width: calc(100% - 32px);
  padding: 12px 18px; border-radius: 10px;
  font-weight: 500; text-align: center;
  color: #fff; background: #2e7d32;
  box-shadow: 0 6px 18px rgba(0, 0, 0, .25);
  cursor: pointer;
}
.toast.error { background: #c62828; }

/* Inserimento PIN */
.pin-box {
  max-width: 360px; margin: 32px auto;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 20px; display: grid; gap: 12px; text-align: center;
}
.pin-box h2 { margin: 0; font-size: 1.2rem; }
.pin-box .hint { margin: 0; }
.pin-box input { text-align: center; font-size: 1.4rem; letter-spacing: .3em; }

/* Intestazione: titolo, famiglia, utente + Esci */
.title-row { flex-wrap: wrap; row-gap: 4px; }
.title-row h1 { flex: 0 1 auto; }
.env-name {
  font-size: .9rem; font-weight: 600;
  color: var(--primary);
  border: 1px solid var(--border); border-radius: 99px;
  padding: 2px 10px;
}
.user-box { margin-left: auto; display: flex; align-items: center; gap: 8px; }
.btn-small { min-height: 36px; padding: 0 14px; font-size: .9rem; font-weight: 600; }
.env-switch { width: auto; min-height: 36px; padding: 4px 8px; font-size: .9rem; font-weight: 600; }
.pill.admin { background: var(--primary); color: var(--primary-text); }

/* Finiti: sezione separata, schede grigie senza colori di scadenza */
.section-title {
  font-size: .95rem; font-weight: 600; color: var(--muted);
  margin: 28px 0 10px; padding-top: 16px;
  border-top: 1px dashed var(--border);
}
.card.closed {
  border-left-color: var(--muted);
  border-style: dashed;
  border-left-style: solid;
  background: transparent;
  color: var(--muted);
}
.card.closed .name { text-decoration: line-through; }
.card.closed .expiry { font-weight: 400; }

/* ===== Admin ===== */
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.text-expired { color: var(--expired); font-weight: 600; }
.danger { background: transparent; color: var(--expired); border: 1px solid var(--expired); }
a.button {
  display: inline-flex; align-items: center; min-height: 44px; padding: 0 16px;
  border-radius: 8px; text-decoration: none; font-weight: 600;
}
a.button.secondary { color: var(--text); }
a.button.btn-small { min-height: 36px; padding: 0 14px; font-size: .9rem; }
a.link { text-decoration: none; display: inline-flex; align-items: center; }
.link.small { font-size: .9rem; justify-self: center; }

.tabs { display: flex; gap: 4px; margin: 4px 0 8px; overflow-x: auto; }
.tabs button {
  background: transparent; color: var(--muted); min-height: 40px; padding: 0 14px;
  border-bottom: 3px solid transparent; border-radius: 0; font-weight: 600;
}
.tabs button.active { color: var(--primary); border-bottom-color: var(--primary); }

.inline-label { display: flex; align-items: center; gap: 8px; width: 100%; font-size: .9rem; color: var(--muted); }
.inline-label select { flex: 1; }

main > section > .filters, .data-panel .filters {
  display: flex; flex-wrap: wrap; align-items: center; gap: 12px; margin-bottom: 12px; font-size: .9rem;
}
main > section > .filters input[type="search"] { flex: 1 1 220px; width: auto; }
select.auto { width: auto; }
.toggle input[type="radio"] { width: 18px; height: 18px; min-height: 0; margin: 0; accent-color: var(--primary); }

.rows { list-style: none; margin: 0 0 16px; padding: 0; display: grid; gap: 8px; }
.row {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 10px 14px;
}
.row.deleted { border-style: dashed; opacity: .8; }
.row-main { display: flex; align-items: center; gap: 12px; }
.row-main > div { flex: 1; min-width: 0; }
.row .pill { margin-left: 6px; background: var(--bg); border: 1px solid var(--border); }
.log-row .pill { margin: 0 6px 0 0; }
.pill.action-create { background: #e3f4e5; color: #1f6b28; border-color: transparent; }
.pill.action-update { background: #e5eefb; color: #1d4f91; border-color: transparent; }
.pill.action-delete { background: var(--expired-bg); color: var(--expired); border-color: transparent; }
.pill.action-restore { background: var(--soon-bg); color: var(--soon); border-color: transparent; }
.pill.action-import { background: var(--primary); color: var(--primary-text); border-color: transparent; }

.changes { margin: 6px 0 0; padding-left: 18px; font-size: .85rem; }
.changes del { color: var(--expired); text-decoration: line-through; }
.changes ins { color: var(--ok); text-decoration: none; font-weight: 600; }

.card-form, .edit-form { display: grid; gap: 10px; }
.card-form {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; margin-bottom: 16px;
}
.card-form h2 { margin: 0; font-size: 1.1rem; }
.card-form label, .edit-form label { display: block; font-size: .85rem; color: var(--muted); }
.card-form label > input, .edit-form label > input { margin-top: 4px; }
.card-form .hint, .card-form p { margin: 0; }
.card-form .actions, .edit-form .actions { margin-top: 4px; }
.data-panel { max-width: 720px; }

/* Link discreto all'admin in fondo all'app */
.footer { text-align: center; padding: 24px 16px 8px; font-size: .85rem; }
.footer a { color: var(--muted); }
.row .pill.expired { background: var(--expired-bg); color: var(--expired); border-color: transparent; }
.row .pill.deleted { background: transparent; color: var(--muted); border-style: dashed; }
