@import url('https://fonts.googleapis.com/css2?family=Figtree:wght@400;500;600;700&family=Literata:opsz,wght@7..72,600&display=swap');

:root {
  --ink: #152028;
  --muted: #667480;
  --bg: #f4f6f5;
  --surface: #ffffff;
  --line: #dde3e0;
  --line-strong: #b8c4bf;
  --brand: #0b5f63;
  --brand-dark: #08484b;
  --brand-soft: #e6f3f2;
  --sea: #2a9d8f;
  --danger: #c1121f;
  --danger-soft: #fdecee;
  --warn: #9a6700;
  --warn-soft: #fff6e0;
  --ok: #1f7a45;
  --ok-soft: #e8f6ee;
  --pending-soft: #eef1f4;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 0 rgba(21, 32, 40, .04), 0 8px 24px rgba(21, 32, 40, .05);
  --font: 'Figtree', system-ui, sans-serif;
  --display: 'Literata', Georgia, serif;
  --tap: 44px;
  --header-h: 3.5rem;
  --bottom-nav-h: 4.25rem;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  color: var(--ink);
  background:
    linear-gradient(180deg, #eaf3f2 0, transparent 180px),
    var(--bg);
  min-height: 100vh;
  line-height: 1.45;
  font-size: 16px;
}
img, video, iframe { max-width: 100%; height: auto; }
a { color: var(--brand); }
a:hover { color: var(--brand-dark); }

.app-shell {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

/* ——— Header ——— */
.topbar {
  position: sticky;
  top: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  gap: .75rem;
  min-height: var(--header-h);
  padding: .5rem .85rem;
  padding-top: max(.5rem, env(safe-area-inset-top));
  background: rgba(255,255,255,.92);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.brand {
  display: flex;
  align-items: center;
  gap: .65rem;
  color: var(--ink);
  text-decoration: none;
  min-width: 0;
  flex: 1;
}
.brand-mark {
  width: 2.1rem;
  height: 2.1rem;
  border-radius: 9px;
  background: linear-gradient(145deg, var(--sea), var(--brand));
  color: #fff;
  display: grid;
  place-items: center;
  font-family: var(--display);
  font-weight: 600;
  font-size: .95rem;
  flex-shrink: 0;
}
.brand-text {
  display: flex;
  flex-direction: column;
  min-width: 0;
}
.brand-text strong {
  font-family: var(--display);
  font-size: 1rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.brand-text small {
  color: var(--muted);
  font-size: .72rem;
  font-weight: 500;
}

.nav-toggle {
  width: var(--tap);
  height: var(--tap);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface);
  display: inline-flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 5px;
  cursor: pointer;
  padding: 0;
}
.nav-toggle span {
  display: block;
  width: 18px;
  height: 2px;
  background: var(--ink);
  border-radius: 1px;
}
.nav-toggle[aria-expanded="true"] span:nth-child(1) { transform: translateY(7px) rotate(45deg); }
.nav-toggle[aria-expanded="true"] span:nth-child(2) { opacity: 0; }
.nav-toggle[aria-expanded="true"] span:nth-child(3) { transform: translateY(-7px) rotate(-45deg); }
.nav-toggle span { transition: transform .2s ease, opacity .2s ease; }

.nav-drawer {
  display: none;
  position: absolute;
  left: 0;
  right: 0;
  top: 100%;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  box-shadow: var(--shadow);
  padding: .5rem;
}
.nav-drawer.is-open { display: block; }
.nav-drawer a,
.nav-drawer .drawer-user {
  display: block;
  padding: .85rem 1rem;
  color: var(--ink);
  text-decoration: none;
  border-radius: var(--radius-sm);
  font-weight: 500;
}
.nav-drawer a:hover { background: var(--brand-soft); }
.nav-drawer .drawer-user {
  color: var(--muted);
  font-size: .9rem;
  border-top: 1px solid var(--line);
  margin-top: .25rem;
  border-radius: 0;
}
.nav-drawer form { padding: .5rem 1rem 1rem; }

.topbar-desktop {
  display: none;
  align-items: center;
  gap: .15rem;
  flex: 1;
}
.topbar-desktop a {
  color: var(--muted);
  text-decoration: none;
  font-weight: 500;
  font-size: .92rem;
  padding: .55rem .7rem;
  border-radius: 999px;
}
.topbar-desktop a:hover {
  color: var(--brand-dark);
  background: var(--brand-soft);
}
.topbar-right {
  display: none;
  align-items: center;
  gap: .75rem;
  font-size: .85rem;
  color: var(--muted);
  white-space: nowrap;
}

.notif-bell {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: var(--tap);
  height: var(--tap);
  border-radius: var(--radius-sm);
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  text-decoration: none;
  flex-shrink: 0;
}
.notif-bell:hover { background: var(--brand-soft); color: var(--brand-dark); }
.notif-count {
  position: absolute;
  top: 4px;
  right: 4px;
  min-width: 1.1rem;
  height: 1.1rem;
  padding: 0 .25rem;
  border-radius: 999px;
  background: var(--danger);
  color: #fff;
  font-size: .65rem;
  font-weight: 700;
  display: grid;
  place-items: center;
  line-height: 1;
}

.notif-list { display: grid; gap: .75rem; }
.notif-item {
  padding: .9rem;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--bg);
}
.notif-item.is-unread {
  background: var(--brand-soft);
  border-color: #c5dedc;
}
.notif-meta {
  display: flex;
  flex-wrap: wrap;
  gap: .5rem;
  align-items: center;
  justify-content: space-between;
  margin-bottom: .35rem;
}
.notif-title {
  font-size: 1rem;
  border: 0;
  padding: 0;
  margin: 0 0 .35rem;
}
.notif-banner {
  margin-bottom: 1rem;
  padding: .9rem 1rem;
  border-radius: var(--radius);
  background: var(--warn-soft);
  border: 1px solid #e6d3a0;
}
.notif-banner a { font-weight: 600; }


/* Patient bottom nav (mobile) */
.bottom-nav {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 40;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: .5rem;
  padding: .55rem .75rem calc(.55rem + var(--safe-bottom));
  background: rgba(255,255,255,.96);
  border-top: 1px solid var(--line);
  box-shadow: 0 -8px 24px rgba(21,32,40,.06);
}
.bottom-nav a {
  min-height: var(--tap);
  display: grid;
  place-items: center;
  text-decoration: none;
  font-weight: 600;
  font-size: .9rem;
  border-radius: var(--radius-sm);
  color: var(--ink);
  background: var(--bg);
  border: 1px solid var(--line);
}
.bottom-nav a.is-emergency {
  background: var(--danger);
  border-color: #9a0e18;
  color: #fff;
}
body.has-bottom-nav .main {
  padding-bottom: calc(var(--bottom-nav-h) + var(--safe-bottom) + 1rem);
}

.main {
  width: min(1080px, calc(100% - 1.5rem));
  margin: 1rem auto 2rem;
  flex: 1;
}

h1, h2, h3 {
  margin: 0 0 .4rem;
  line-height: 1.25;
  color: var(--ink);
  font-weight: 700;
}
h1 {
  font-family: var(--display);
  font-size: clamp(1.35rem, 4vw, 1.75rem);
  font-weight: 600;
}
h2 {
  font-size: 1.05rem;
  padding-bottom: .5rem;
  margin-bottom: .85rem;
  border-bottom: 1px solid var(--line);
}
.lead {
  color: var(--muted);
  margin: 0 0 1rem;
  font-size: .95rem;
}
.page-head { margin-bottom: 1rem; }
.page-head-row {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  gap: .75rem;
  flex-wrap: wrap;
  margin-bottom: 1rem;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 1rem;
  box-shadow: var(--shadow);
}

.grid { display: grid; gap: .85rem; }
.grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr; }

.kpi {
  padding: .9rem 1rem;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
.kpi .label {
  font-size: .72rem;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--muted);
  font-weight: 700;
}
.kpi .value {
  font-size: clamp(1.4rem, 5vw, 1.7rem);
  margin-top: .2rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.badge {
  display: inline-block;
  padding: .2rem .45rem;
  border-radius: 6px;
  font-size: .72rem;
  font-weight: 700;
  letter-spacing: .02em;
  text-transform: uppercase;
}
.badge-verde { background: var(--ok-soft); color: var(--ok); }
.badge-giallo { background: var(--warn-soft); color: var(--warn); }
.badge-rosso { background: var(--danger-soft); color: var(--danger); }
.badge-pending { background: var(--pending-soft); color: var(--muted); }

.table-wrap {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
}
.table-wrap table { border: 0; }
table { width: 100%; border-collapse: collapse; font-size: .92rem; }
th, td {
  text-align: left;
  padding: .7rem .65rem;
  border-bottom: 1px solid var(--line);
  vertical-align: top;
}
th {
  font-size: .7rem;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--muted);
  font-weight: 700;
  background: #f7faf9;
  white-space: nowrap;
}
tbody tr:last-child td { border-bottom: 0; }

/* Mobile card tables */
@media (max-width: 720px) {
  .table-cards thead { display: none; }
  .table-cards,
  .table-cards tbody,
  .table-cards tr,
  .table-cards td {
    display: block;
    width: 100%;
  }
  .table-cards tr {
    padding: .85rem;
    border-bottom: 1px solid var(--line);
  }
  .table-cards tr:last-child { border-bottom: 0; }
  .table-cards td {
    border: 0;
    padding: .2rem 0;
    display: grid;
    grid-template-columns: 7.5rem 1fr;
    gap: .5rem;
  }
  .table-cards td::before {
    content: attr(data-label);
    color: var(--muted);
    font-size: .75rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: .03em;
  }
  .table-cards td[data-label=""] {
    grid-template-columns: 1fr;
    padding-top: .55rem;
  }
  .table-cards td[data-label=""]::before { display: none; }
  .table-wrap.table-cards-wrap {
    border: 0;
    overflow: visible;
  }
  .table-wrap.table-cards-wrap table {
    border: 1px solid var(--line);
    border-radius: var(--radius);
    overflow: hidden;
    background: var(--surface);
  }
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: .35rem;
  min-height: var(--tap);
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  padding: .65rem 1rem;
  font: inherit;
  font-size: .95rem;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  touch-action: manipulation;
}
.btn:hover { text-decoration: none; }
.btn-primary { background: var(--brand); color: #fff; }
.btn-primary:hover { background: var(--brand-dark); color: #fff; }
.btn-ghost { background: var(--surface); border-color: var(--line-strong); color: var(--ink); }
.btn-ghost:hover { background: var(--bg); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-amber { background: var(--warn); color: #fff; }
.btn-block { width: 100%; }
.btn-emergency {
  display: flex;
  width: 100%;
  max-width: none;
  background: var(--danger);
  color: #fff;
  border: 0;
  border-radius: var(--radius);
  font-size: 1.05rem;
  min-height: 3.25rem;
  box-shadow: 0 10px 24px rgba(193, 18, 31, .22);
}

.form-row { margin-bottom: .9rem; }
label {
  display: block;
  font-size: .85rem;
  font-weight: 600;
  margin-bottom: .35rem;
}
input, select, textarea {
  width: 100%;
  min-height: var(--tap);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  padding: .7rem .85rem;
  font: inherit;
  font-size: 16px; /* evita zoom iOS */
  background: #fff;
  color: var(--ink);
}
textarea { min-height: 110px; resize: vertical; }
input:focus, select:focus, textarea:focus {
  outline: 2px solid rgba(11, 95, 99, .28);
  border-color: var(--brand);
}
.actions {
  display: flex;
  flex-wrap: wrap;
  gap: .55rem;
  margin-top: .9rem;
}
.actions .btn { flex: 1 1 auto; }

.flash {
  padding: .85rem 1rem;
  border-radius: var(--radius-sm);
  margin-bottom: .9rem;
  font-size: .95rem;
}
.flash-success { background: var(--ok-soft); color: var(--ok); }
.flash-error { background: var(--danger-soft); color: var(--danger); }

.steps {
  display: grid;
  gap: .65rem;
}
.step {
  display: grid;
  grid-template-columns: 2.25rem 1fr;
  grid-template-areas:
    "num body"
    "num action";
  gap: .35rem .75rem;
  padding: 1rem;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
.step-num { grid-area: num; }
.step > div:nth-child(2) { grid-area: body; }
.step > div:nth-child(3) {
  grid-area: action;
  justify-self: start;
}
.step.locked { opacity: .7; }
.step.done {
  background: var(--ok-soft);
  border-color: #c7e6d3;
}
.step-num {
  width: 2.1rem;
  height: 2.1rem;
  border-radius: 10px;
  display: grid;
  place-items: center;
  background: var(--brand);
  color: #fff;
  font-weight: 700;
}
.step.locked .step-num { background: #9aa7a5; }

.kit-box {
  background: var(--brand-soft);
  border: 1px solid #c5dedc;
  border-radius: var(--radius-sm);
  padding: .9rem 1rem;
  font-size: .95rem;
}

.login-wrap {
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  grid-template-columns: 1fr;
  background:
    radial-gradient(600px 280px at 50% -10%, #cfe8e5, transparent),
    var(--bg);
}
.login-side {
  padding: 1.5rem 1.25rem 1rem;
}
.login-side h1 {
  color: var(--ink);
  margin-bottom: .5rem;
}
.login-side p { color: var(--muted); margin: 0; }
.login-side .meta {
  display: none;
  font-size: .8rem;
  color: var(--muted);
  margin-top: 1.5rem;
}
.login-main {
  padding: 0 1rem 2rem;
  display: flex;
  justify-content: center;
}
.login-card {
  width: min(420px, 100%);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: calc(var(--radius) + 4px);
  padding: 1.25rem;
  box-shadow: var(--shadow);
}
.login-card h2 { border: 0; padding: 0; margin-bottom: 1rem; }
.hint { font-size: .8rem; color: var(--muted); margin-top: 1rem; line-height: 1.45; }

.emergency-body {
  margin: 0;
  background: #fff5f5;
  color: var(--ink);
  font-family: var(--font);
  min-height: 100vh;
  min-height: 100dvh;
}
.emergency-banner {
  background: var(--danger);
  color: #fff;
  padding: 1.1rem 1rem;
  padding-top: max(1.1rem, env(safe-area-inset-top));
}
.emergency-banner h1 { color: #fff; margin: 0; font-size: clamp(1.25rem, 5vw, 1.55rem); }
.emergency-banner p { margin: .4rem 0 0; opacity: .95; font-size: .95rem; }
.emergency-wrap {
  width: min(640px, calc(100% - 1.25rem));
  margin: 0 auto;
  padding: .75rem 0 calc(2rem + var(--safe-bottom));
}
.e-step {
  margin: .7rem 0;
  padding: 1rem;
  background: #fff;
  border: 1px solid #f0c9cc;
  border-radius: var(--radius);
  border-left: 5px solid var(--danger);
}
.e-step strong {
  display: block;
  font-size: 1.05rem;
  margin-bottom: .35rem;
}
.countdown {
  font-family: var(--display);
  font-size: clamp(2.5rem, 12vw, 3.4rem);
  text-align: center;
  margin: .5rem 0;
  color: var(--danger);
}
.call-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 3.25rem;
  text-align: center;
  background: var(--danger);
  color: #fff;
  font-weight: 700;
  font-size: 1.1rem;
  padding: .9rem;
  border-radius: var(--radius-sm);
  text-decoration: none;
  margin-top: .65rem;
}
.call-btn.secondary { background: #9a0e18; }
.back-safe {
  display: inline-flex;
  align-items: center;
  min-height: var(--tap);
  color: var(--brand);
  font-weight: 600;
  text-decoration: none;
}

.stack-gap > * + * { margin-top: .85rem; }
.muted { color: var(--muted); font-size: .9rem; }
.video-frame {
  aspect-ratio: 16/9;
  width: 100%;
  border: 0;
  border-radius: var(--radius-sm);
  background: #152028;
}
.check-item {
  display: flex;
  gap: .75rem;
  align-items: flex-start;
  padding: .85rem 0;
  border-bottom: 1px solid var(--line);
}
.check-item input {
  width: 1.25rem;
  height: 1.25rem;
  min-height: 0;
  margin-top: .15rem;
  flex-shrink: 0;
}
#reader {
  width: 100%;
  max-width: 420px;
  margin: 0 auto 1rem;
  border-radius: var(--radius-sm);
  overflow: hidden;
  border: 1px solid var(--line);
}

/* ——— Breakpoints ——— */
@media (min-width: 640px) {
  .grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .step {
    grid-template-columns: 2.25rem 1fr auto;
    grid-template-areas: "num body action";
    align-items: center;
  }
  .step > div:nth-child(3) { justify-self: end; }
  .actions .btn { flex: 0 1 auto; }
  .btn-emergency { max-width: 26rem; }
}

@media (min-width: 900px) {
  .grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .nav-toggle, .nav-drawer, .bottom-nav { display: none !important; }
  body.has-bottom-nav .main { padding-bottom: 2rem; }
  .topbar {
    padding: .65rem 1.25rem;
    gap: 1rem;
  }
  .brand { flex: 0 0 auto; max-width: 16rem; }
  .topbar-desktop { display: flex; }
  .topbar-right { display: flex; }
  .notif-bell { order: 0; }
  .login-wrap {
    grid-template-columns: minmax(16rem, 28rem) 1fr;
    background: var(--bg);
  }
  .login-side {
    background: linear-gradient(165deg, var(--brand-dark), var(--brand) 55%, var(--sea));
    color: #fff;
    padding: 2.5rem 2rem;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    min-height: 100vh;
    min-height: 100dvh;
  }
  .login-side h1, .login-side p { color: #fff; }
  .login-side p { opacity: .9; }
  .login-side .meta { display: block; color: rgba(255,255,255,.7); }
  .login-main {
    align-items: center;
    padding: 2rem;
  }
}

@media (min-width: 1100px) {
  .main { width: min(1100px, calc(100% - 2.5rem)); margin-top: 1.5rem; }
}
