:root {
  --bg: #f5f6f7;
  --surface: #ffffff;
  --ink: #1b2327;
  --muted: #6b7a82;
  --line: #dde3e6;
  --accent: #0f766e;
  --accent-ink: #ffffff;
  --warn: #b45309;
  --warn-bg: #fef3c7;
  --info-bg: #e0f2fe;
  --radius: 10px;
}

* { box-sizing: border-box; }

html { font-size: 15px; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  line-height: 1.45;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

h1 { font-size: 1.55rem; margin: 0 0 .25rem; letter-spacing: -.01em; }
h2 { font-size: 1.02rem; margin: 0 0 .7rem; }

.wrap { max-width: 1400px; margin: 0 auto; padding: 0 1.2rem; }

/* ---------------------------------------------------------------- topbar */
.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 20;
}
.topbar-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 56px;
}
.brand {
  display: flex;
  align-items: center;
  gap: .8rem;
  color: var(--ink);
  min-width: 0;
}
.brand:hover { text-decoration: none; }
.brand-text { display: flex; flex-direction: column; min-width: 0; }
.brand-title {
  font-size: 1.85rem;
  font-weight: 800;
  letter-spacing: -.02em;
  line-height: 1.05;
  white-space: nowrap;
}
.brand small {
  display: block;
  font-size: .72rem;
  font-weight: 500;
  color: var(--muted);
  letter-spacing: .08em;
  text-transform: uppercase;
}
.logo { width: 48px; height: 48px; color: var(--accent); flex: 0 0 auto; }
.topbar-right {
  display: flex;
  align-items: center;
  gap: 1.1rem;
  margin-left: auto;
  flex-wrap: wrap;
  justify-content: flex-end;
}
.nav { display: flex; gap: .2rem; flex-wrap: wrap; }
@media (max-width: 1000px) {
  .brand-title { font-size: 1.35rem; }
  .logo { width: 38px; height: 38px; }
  .topbar-right { width: 100%; justify-content: flex-start; gap: .6rem; }
}
@media (max-width: 560px) {
  .brand-title { font-size: 1.05rem; white-space: normal; }
  .logo { width: 30px; height: 30px; }
}
.nav a {
  padding: .45rem .8rem;
  border-radius: 999px;
  color: var(--muted);
  font-weight: 600;
  font-size: .9rem;
}
.nav a:hover { background: var(--bg); text-decoration: none; }
.nav a.active { background: #e6f2f1; color: var(--accent); }
.tag-soon {
  font-size: .62rem;
  text-transform: uppercase;
  letter-spacing: .05em;
  background: var(--warn-bg);
  color: var(--warn);
  padding: .05rem .35rem;
  border-radius: 4px;
  vertical-align: 1px;
}

main.wrap { padding-top: 1.4rem; padding-bottom: 3rem; }

/* ---------------------------------------------------------------- layout */
.layout {
  display: grid;
  grid-template-columns: 220px minmax(0, 1fr);
  gap: 1.4rem;
  align-items: start;
  padding-top: 1.3rem;
  padding-bottom: 3rem;
}
.layout.no-sidebar { grid-template-columns: minmax(0, 1fr); }
.content { min-width: 0; }

.sidebar { position: sticky; top: 92px; }
.sidenav {
  display: flex;
  flex-direction: column;
  gap: .15rem;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: .45rem;
}
.sidenav a {
  display: block;
  padding: .62rem .75rem;
  border-radius: 7px;
  border-left: 3px solid transparent;
  color: var(--muted);
  font-weight: 600;
  font-size: .9rem;
}
.sidenav a:hover { background: var(--bg); color: var(--ink); text-decoration: none; }
.sidenav a.active {
  background: #e6f2f1;
  color: var(--accent);
  border-left-color: var(--accent);
}

.sidebar-user {
  margin-top: .8rem;
  padding: .75rem .8rem;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  display: flex;
  flex-direction: column;
  gap: .35rem;
  font-size: .82rem;
}
.sidebar-label {
  font-size: .66rem;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--muted);
}
.sidebar-username { font-weight: 700; }
.sidebar-links { display: flex; gap: .8rem; flex-wrap: wrap; font-size: .8rem; }

@media (max-width: 900px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
  .sidebar { position: static; }
  .sidenav { flex-direction: column; }
}

/* ---------------------------------------------------------------- testate */
.page-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1rem;
  flex-wrap: wrap;
}
.breadcrumb { font-size: .8rem; color: var(--muted); margin-bottom: .15rem; }
.head-actions { display: flex; gap: .45rem; flex-wrap: wrap; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.pill {
  display: inline-block;
  font-size: .7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--accent);
  background: #e6f2f1;
  border-radius: 999px;
  padding: .05rem .45rem;
}

/* ---------------------------------------------------------------- bottoni */
.btn {
  display: inline-block;
  border: 1px solid transparent;
  border-radius: 8px;
  padding: .5rem .85rem;
  font-size: .88rem;
  font-weight: 600;
  cursor: pointer;
  background: var(--surface);
  color: var(--ink);
  line-height: 1.2;
}
.btn:hover { text-decoration: none; }
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { background: #0b5f58; }
.btn.ghost { border-color: var(--line); }
.btn.ghost:hover { background: #eef1f2; }
.btn.mini { padding: .33rem .6rem; font-size: .8rem; }
.btn:disabled { opacity: .45; cursor: not-allowed; }

/* ---------------------------------------------------------------- card */
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 1rem 1.1rem;
  margin-bottom: 1rem;
}

/* ---------------------------------------------------------------- filtri */
.filters { padding: .9rem 1rem; }
.filter-grid {
  display: grid;
  grid-template-columns: repeat(12, 1fr);
  gap: .6rem .8rem;
  align-items: end;
}
.fg { display: flex; flex-direction: column; gap: .22rem; grid-column: span 3; }
.fg.wide { grid-column: span 6; }
.fg.narrow { grid-column: span 2; }
.fg.xs { grid-column: span 1; }
.fg > span { font-size: .72rem; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
input[type="text"], input[type="search"], input[type="date"], input[type="number"], select, textarea {
  width: 100%;
  padding: .42rem .55rem;
  border: 1px solid var(--line);
  border-radius: 7px;
  font: inherit;
  font-size: .88rem;
  background: #fff;
  color: var(--ink);
}
input:focus, select:focus, textarea:focus { outline: 2px solid #9ad4cf; border-color: var(--accent); }
.check { display: flex; align-items: center; gap: .4rem; font-size: .85rem; grid-column: span 3; }
.check input { width: auto; }
.filter-actions { grid-column: span 12; display: flex; justify-content: flex-end; gap: .5rem; }
@media (max-width: 1100px) {
  .fg, .fg.wide, .fg.narrow { grid-column: span 4; }
  .fg.xs { grid-column: span 2; }
}
@media (max-width: 720px) {
  .fg, .fg.wide, .fg.narrow, .fg.xs, .check { grid-column: span 12; }
}

.chips { display: flex; flex-wrap: wrap; gap: .35rem; margin: -.3rem 0 1rem; }
.chip {
  font-size: .78rem;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: .18rem .6rem;
  color: #33464d;
}

/* ---------------------------------------------------------------- bulkbar */
.bulkbar {
  display: flex;
  align-items: center;
  gap: .5rem;
  flex-wrap: wrap;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: .55rem .8rem;
  margin-bottom: .7rem;
  font-size: .85rem;
}
.bulkbar .sep { flex: 1; }

/* ---------------------------------------------------------------- tabella */
.table-card { padding: 0; overflow: auto; }
.grid { width: 100%; border-collapse: collapse; font-size: .87rem; }
.grid thead th {
  position: sticky;
  top: 0;
  background: #f0f3f4;
  text-align: left;
  font-size: .72rem;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--muted);
  padding: .55rem .6rem;
  border-bottom: 1px solid var(--line);
  white-space: nowrap;
}
.grid td { padding: .5rem .6rem; border-bottom: 1px solid #edf1f2; vertical-align: top; }
.grid tbody tr:hover { background: #fafcfc; }
.grid tbody tr:last-child td { border-bottom: 0; }
.c-check { width: 30px; }
.c-date { white-space: nowrap; }
.c-sex { width: 52px; text-align: center; }
.c-num { text-align: right; width: 70px; }
.c-mail { width: 56px; text-align: center; }
.plink { font-weight: 650; color: var(--ink); }
.plink:hover { color: var(--accent); }
.sub { font-size: .75rem; color: var(--muted); margin-top: .1rem; }
.empty { text-align: center; color: var(--muted); padding: 1.6rem !important; }

.sniprow td { background: #fbfdf7; border-bottom: 1px solid #edf1f2; padding-top: .1rem; }
.snip { font-size: .8rem; color: #41545b; margin: .12rem 0 .18rem; }
.snip mark { background: #fff1a8; padding: 0 .1em; border-radius: 2px; }

.badge {
  display: inline-block;
  font-size: .7rem;
  font-weight: 700;
  border-radius: 5px;
  padding: .08rem .4rem;
  background: #eef1f2;
  color: #37484f;
  white-space: nowrap;
}
.badge.warn { background: var(--warn-bg); color: var(--warn); }
.badge.quiet { background: #eef1f2; color: var(--muted); font-weight: 600; }
.badge.ok { color: #15803d; }
.badge.t-esame_obiettivo { background: #e0f2fe; color: #075985; }
.badge.t-semeiotica { background: #ede9fe; color: #5b21b6; }
.badge.t-chirurgia { background: #fee2e2; color: #991b1b; }
.badge.t-esame_anamnesi { background: #e6f2f1; color: #0f766e; }
.badge.t-anamnesi { background: #f0fdf4; color: #166534; }
.badge.t-topografia { background: #fef3c7; color: #92400e; }
.badge.t-tonografia { background: #f1f5f9; color: #475569; }
.ok { color: #15803d; font-weight: 700; }

.pager { display: flex; align-items: center; gap: .8rem; justify-content: center; margin: 1.2rem 0; }

/* ---------------------------------------------------------------- scheda */
.anagrafica { padding: 1rem 1.1rem 1.15rem; }
.fields {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: .5rem .9rem;
  margin: 0;
}
.fields > div { border-left: 2px solid #edf1f2; padding-left: .55rem; }
.fields dt { font-size: .7rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.fields dd { margin: .05rem 0 0; font-size: .88rem; }
.fields.dense { grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); }
.fields.dense > div.wide { grid-column: 1 / -1; }
.pre { white-space: pre-wrap; word-break: break-word; }
.mono { font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; font-size: .82rem; }
.note { margin-top: .8rem; padding-top: .6rem; border-top: 1px solid var(--line); font-size: .88rem; }

.counters { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: .5rem; margin-bottom: 1rem; }
.counter {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: .55rem .7rem;
  display: flex;
  flex-direction: column;
}
.counter .n { font-size: 1.2rem; font-weight: 750; }
.counter .l { font-size: .7rem; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; }
.counter.zero { opacity: .5; }

.visitday { margin-bottom: 1.1rem; }
.visitday-head {
  display: flex;
  align-items: baseline;
  gap: .7rem;
  border-bottom: 2px solid var(--accent);
  padding-bottom: .25rem;
  margin-bottom: .55rem;
}
.visitday-head h2 { margin: 0; font-size: 1rem; letter-spacing: .01em; }

.ev {
  background: var(--surface);
  border: 1px solid var(--line);
  border-left: 3px solid #cbd5d9;
  border-radius: 8px;
  padding: .55rem .7rem;
  margin-bottom: .45rem;
}
.ev header { display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; }
.ev-title { font-weight: 600; font-size: .9rem; }
.ev .diagnosi { margin: .35rem 0 .2rem; font-size: .9rem; }
.ev.compact { display: flex; align-items: baseline; gap: .5rem; flex-wrap: wrap; padding: .35rem .6rem; background: #fbfcfc; }
.ev.compact .ev-title { font-weight: 500; }
.ev details { margin-top: .35rem; }
.ev summary { cursor: pointer; font-size: .78rem; color: var(--muted); user-select: none; }
.ev summary:hover { color: var(--accent); }
.ev .fields { margin-top: .5rem; }

/* ---------------------------------------------------------------- kpi / email */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: .6rem; margin-bottom: 1rem; }
.kpi {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: .7rem .85rem;
  display: flex;
  flex-direction: column;
}
.kpi .n { font-size: 1.35rem; font-weight: 750; }
.kpi .l { font-size: .72rem; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; }

.two-col { display: grid; grid-template-columns: minmax(320px, 1fr) minmax(320px, 1fr); gap: 1rem; }
@media (max-width: 900px) { .two-col { grid-template-columns: 1fr; } }

.form-email label.fg { grid-column: auto; margin-bottom: .6rem; }
.form-email textarea { resize: vertical; font-family: inherit; }
.check-row { display: flex; flex-direction: column; gap: .25rem; margin: .3rem 0 .8rem; }
.check-row .check { grid-column: auto; }
.form-foot { display: flex; align-items: center; gap: .6rem; flex-wrap: wrap; }
.todo { margin: .2rem 0 .6rem; padding-left: 1.1rem; font-size: .88rem; }
.todo li { margin-bottom: .25rem; }

.alert { border-radius: var(--radius); padding: .7rem .9rem; margin-bottom: 1rem; font-size: .9rem; border: 1px solid; }
.alert.warn { background: var(--warn-bg); border-color: #fcd34d; color: #713f12; }
.alert.info { background: var(--info-bg); border-color: #7dd3fc; color: #075985; }

.footer {
  border-top: 1px solid var(--line);
  background: var(--surface);
  color: var(--muted);
  font-size: .74rem;
  padding: .8rem 0;
}
.footer code { font-size: .72rem; }

/* ---------------------------------------------------------------- utenti */
.topbar-inner { min-height: 56px; height: auto; padding-top: .35rem; padding-bottom: .35rem; flex-wrap: wrap; gap: .4rem; }
.userbox { display: flex; align-items: center; gap: .45rem; font-size: .82rem; }
.userbox .user-name { font-weight: 700; }
.inline { display: inline; }
.linkbtn {
  background: none;
  border: 0;
  padding: 0;
  color: var(--accent);
  font: inherit;
  font-size: .82rem;
  cursor: pointer;
}
.linkbtn:hover { text-decoration: underline; }
.badge.role-admin { background: #fee2e2; color: #991b1b; }
.badge.role-operatore { background: #e0f2fe; color: #075985; }
.badge.role-visualizzatore { background: #eef1f2; color: #475569; }

.flash-wrap { padding-top: .9rem; }
.flash-wrap .alert { margin-bottom: .5rem; }
.alert.err { background: #fee2e2; border-color: #fca5a5; color: #7f1d1d; }
.alert.ok { background: #dcfce7; border-color: #86efac; color: #14532d; }

.btn.danger { border-color: #fca5a5; color: #991b1b; background: #fff; }
.btn.danger:hover { background: #fef2f2; }
.btn.block { width: 100%; }

.auth-wrap { display: flex; justify-content: center; padding: 3rem 0; }
.auth-card { width: 100%; max-width: 420px; padding: 1.6rem 1.5rem; }
.auth-card h1 { font-size: 1.3rem; }
.auth-form { margin-top: 1rem; }
.auth-form .fg { margin-bottom: .8rem; }
.auth-form .btn { margin-top: .4rem; }

.user-new {
  display: grid;
  grid-template-columns: 1.2fr 1.2fr 1fr auto;
  gap: .6rem;
  align-items: end;
}
@media (max-width: 900px) { .user-new { grid-template-columns: 1fr; } }

.admin-grid td { vertical-align: middle; }
.admin-grid select { width: auto; padding: .25rem .35rem; font-size: .82rem; }
.actions-cell { min-width: 330px; }
.actions-cell form { display: inline-flex; align-items: center; gap: .25rem; margin: 0 .35rem .3rem 0; }
.input-inline { width: 135px !important; padding: .25rem .4rem !important; font-size: .8rem !important; }

.refresh-box { margin-top: .9rem; }
.refresh-status { display: flex; align-items: baseline; gap: .5rem; font-size: .9rem; }
.filter-inline { display: flex; gap: .7rem; align-items: end; flex-wrap: wrap; }
.filter-inline .fg { min-width: 180px; }
.filter-inline .btn { margin-bottom: .1rem; }
.log {
  margin: .5rem 0 0;
  padding: .6rem .7rem;
  background: #0f172a;
  color: #d7e3ea;
  border-radius: 8px;
  font-size: .76rem;
  max-height: 260px;
  overflow: auto;
  white-space: pre-wrap;
}

/* ---------------------------------------------------------------- stampa */
.print-header { display: none; }
.print-filters { font-size: .78rem; color: #444; margin: 0 0 .6rem; }

@media print {
  @page { size: A4 landscape; margin: 12mm; }
  html { font-size: 10pt; }
  body { background: #fff; }
  .no-print, .topbar, .footer, .filters, .chips, .bulkbar, .pager,
  .head-actions, .alert.no-print, button, .tag-soon, .breadcrumb { display: none !important; }
  .wrap { max-width: none; padding: 0; }
  main.wrap { padding-top: 0; }
  .layout { display: block; padding: 0; }
  .card, .ev { border: 1px solid #ccc; box-shadow: none; }
  .print-header { display: flex; justify-content: space-between; font-size: .95rem; border-bottom: 2px solid #000; padding-bottom: .3rem; margin-bottom: .5rem; }
  .grid thead th { position: static; background: #eee !important; color: #000; }
  .grid { font-size: .8rem; }
  .grid th, .grid td { border: 1px solid #bbb; padding: .25rem .35rem; }
  .grid tbody tr { page-break-inside: avoid; }
  .visitday-head { border-bottom-color: #000; }
  .ev details { display: block; }
  .ev details > summary { display: none; }
  .ev details > * { display: block; }
  a { color: #000; text-decoration: none; }
  .badge { border: 1px solid #999; background: #fff !important; color: #000 !important; }
}
