:root {
  --primary: #1565c0;
  --primary-dark: #0d47a1;
  --danger: #d32f2f;
  --success: #2e7d32;
  --warning: #f57c00;
  --bg: #f2f4f7;
  --card-bg: #ffffff;
  --text: #212529;
  --muted: #6c757d;
  --border: #e1e4e8;
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; }
html { overflow-x: hidden; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  overscroll-behavior-y: contain;
  overflow-x: hidden;
  width: 100%;
  max-width: 100vw;
}
img, svg { max-width: 100%; }

.hidden { display: none !important; }

/* ---- Login ---- */
.screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: linear-gradient(160deg, var(--primary), var(--primary-dark));
}
.login-card {
  background: var(--card-bg);
  border-radius: var(--radius);
  padding: 28px 22px;
  width: 100%;
  max-width: 360px;
  box-shadow: 0 10px 30px rgba(0,0,0,.25);
}
.login-card h1 {
  text-align: center;
  font-size: 22px;
  margin: 0 0 20px;
  color: var(--primary-dark);
}
.login-card h1 small { font-size: 14px; color: var(--muted); font-weight: 400; }
.login-card label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  margin-bottom: 14px;
}
.login-card input {
  display: block;
  width: 100%;
  margin-top: 6px;
  padding: 12px;
  font-size: 16px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #f8f9fa;
}
.error-msg { color: var(--danger); font-size: 13px; min-height: 18px; margin-bottom: 6px; text-align: center; font-weight: 600; }

/* ---- Buttons ---- */
.btn {
  display: inline-block;
  padding: 12px 18px;
  border: none;
  border-radius: 10px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}
.btn-block { width: 100%; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-warning { background: var(--warning); color: #fff; }
.btn-success { background: var(--success); color: #fff; }
.btn-secondary { background: #e9ecef; color: var(--text); }
.icon-btn {
  background: none;
  border: none;
  font-size: 20px;
  padding: 8px 10px;
  cursor: pointer;
  color: inherit;
  line-height: 1;
}

/* ---- App shell ---- */
.app { min-height: 100vh; display: flex; flex-direction: column; }
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between;
  background: var(--primary);
  color: #fff;
  padding: calc(10px + var(--safe-top)) 6px 10px;
}
.topbar-title { font-size: 17px; font-weight: 700; }

.views { flex: 1; padding: 12px 12px calc(84px + var(--safe-bottom)); overflow-y: auto; }
.view { display: none; }
.view.active { display: block; }

.bottom-nav {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 20;
  display: flex;
  background: #fff;
  border-top: 1px solid var(--border);
  padding-bottom: var(--safe-bottom);
}
.bn-btn {
  flex: 1;
  background: none; border: none;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 8px 0 6px;
  font-size: 11px;
  color: var(--muted);
}
.bn-btn.active { color: var(--primary); }
.bn-icon { font-size: 20px; }

/* ---- Drawer ---- */
.drawer-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.4);
  z-index: 29; opacity: 0; pointer-events: none; transition: opacity .2s;
}
.drawer-overlay.open { opacity: 1; pointer-events: auto; }
.drawer {
  position: fixed; top: 0; bottom: 0; left: 0;
  width: 78%; max-width: 300px;
  background: #fff;
  z-index: 30;
  transform: translateX(-100%);
  transition: transform .25s ease;
  display: flex; flex-direction: column;
  padding-top: var(--safe-top);
  box-shadow: 2px 0 12px rgba(0,0,0,.2);
}
.drawer.open { transform: translateX(0); }
.drawer-header { padding: 20px 18px 14px; border-bottom: 1px solid var(--border); }
.drawer-user-name { font-weight: 700; font-size: 16px; }
.muted { color: var(--muted); font-size: 13px; }
.drawer-list { list-style: none; margin: 0; padding: 6px 0; flex: 1; overflow-y: auto; }
.drawer-list li {
  display: flex; align-items: center; gap: 12px;
  padding: 14px 18px;
  font-size: 15px;
  border-bottom: 1px solid #f1f2f4;
}
.drawer-list li span { font-size: 18px; width: 22px; text-align: center; }
.drawer-list li.active { background: #e8f0fe; color: var(--primary-dark); font-weight: 700; }
.drawer-logout { margin: 14px; }

/* ---- Cards ---- */
.card-list { display: flex; flex-direction: column; gap: 10px; }
.card {
  background: var(--card-bg);
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: 0 1px 3px rgba(0,0,0,.08);
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
}
.card-main { flex: 1; min-width: 0; }
.card-title { font-weight: 700; font-size: 15px; }
.card-sub { color: var(--muted); font-size: 13px; margin-top: 2px; }
.card-badge {
  font-size: 13px; font-weight: 700;
  padding: 6px 12px; border-radius: 20px;
  background: #e8f0fe; color: var(--primary-dark);
  white-space: nowrap;
}
.card-badge.empty { background: #f1f2f4; color: var(--muted); }
.status-pill { font-size: 11px; font-weight: 700; padding: 3px 9px; border-radius: 12px; }
.status-attivo { background: #e6f4ea; color: var(--success); }
.status-inattivo { background: #fdecea; color: var(--danger); }
.status-futuro { background: #fff4e5; color: var(--warning); }
.status-pill.stato-pendente { background: #fff4e5; color: var(--warning); }
.status-pill.stato-accettata { background: #e6f4ea; color: var(--success); }
.status-pill.stato-rifiutata { background: #fdecea; color: var(--danger); }

/* ---- Day / month nav ---- */
.day-nav {
  display: flex; align-items: center; gap: 2px;
  background: #fff; border-radius: var(--radius);
  padding: 6px 4px; margin-bottom: 10px;
  box-shadow: 0 1px 3px rgba(0,0,0,.08);
}
.day-label { font-weight: 700; font-size: 13px; text-align: center; flex: 1; text-transform: capitalize; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.stato-mese-badge {
  font-size: 10px; font-weight: 700; padding: 3px 8px; border-radius: 10px;
  flex: 0 0 auto; white-space: nowrap;
}
.stato-mese-badge:empty { display: none; }
.stato-mese-badge.mese-confermato { background: #e6f4ea; color: var(--success); }
.stato-mese-badge.mese-parziale { background: #fff4e5; color: #b26a00; }
.stato-mese-badge.mese-non-confermato { background: #e8f0fe; color: var(--primary-dark); }
.stato-mese-badge.mese-vuoto { background: #f1f2f4; color: var(--muted); }
.stato-mese-badge.versione-uff { background: #8e44ad; color: #fff; margin-left: 4px; }

.version-btn { font-size: 11px; font-weight: 700; padding: 5px 12px; border-radius: 16px; color: #fff; background: var(--muted); }
.version-btn.active-ope { background: var(--success); box-shadow: 0 0 6px rgba(40,167,69,.5); }
.version-btn.active-uff { background: #0d6efd; box-shadow: 0 0 6px rgba(13,110,253,.5); }
.day-nav .icon-btn { font-size: 16px; padding: 6px 8px; flex: 0 0 auto; }
.day-nav .icon-btn:disabled { opacity: .35; cursor: not-allowed; }
.btn-azione {
  font-size: 12.5px; font-weight: 700; color: var(--text);
  background: #f1f2f4; border-radius: 18px; padding: 7px 12px;
  white-space: nowrap;
}
.btn-salva {
  font-size: 12.5px; font-weight: 700; color: var(--muted);
  background: #f1f2f4; border-radius: 18px; padding: 7px 12px;
  white-space: nowrap;
}
.btn-salva.btn-dirty { background: var(--success); color: #fff; }
.btn-salva:disabled, .btn-azione:disabled { opacity: .45; }
.zoom-text-label { font-size: 11px; font-weight: 700; color: var(--muted); margin-left: 4px; }
.day-nav.toolbar-row { justify-content: flex-end; margin-top: -2px; flex-wrap: wrap; row-gap: 4px; }
.day-nav.toolbar-row.toolbar-wrap { justify-content: flex-start; align-items: center; gap: 6px; row-gap: 8px; }
.zoom-control {
  display: flex; align-items: center; gap: 2px;
  background: #f1f2f4; border-radius: 20px; padding: 2px; margin-left: 6px;
}
.zoom-control .icon-btn { font-size: 15px; padding: 4px 9px; }
.zoom-label { font-size: 11px; font-weight: 700; color: var(--muted); width: 16px; text-align: center; }

/* ---- Selettore vista Calendario (Medici / Turni) + zoom, sulla stessa riga ---- */
.vista-row { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.view-switch {
  display: flex; background: #f1f2f4; border-radius: 20px; padding: 3px; gap: 2px;
  flex: 1 1 auto; min-width: 0;
}
.vs-option {
  flex: 1; text-align: center; font-size: 13px; font-weight: 700; color: var(--muted);
  padding: 7px 0; border-radius: 16px; cursor: pointer; transition: background .15s, color .15s;
}
.vs-option input { display: none; }
.vs-option.active { background: #fff; color: var(--primary-dark); box-shadow: 0 1px 3px rgba(0,0,0,.15); }
.vista-row .zoom-control { flex: 0 0 auto; margin-left: 0; }
.zoom-control.disabled { opacity: .4; pointer-events: none; }

/* ---- Griglia Calendario (medici x giorni) ---- */
.grid-wrap {
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  background: #fff;
  border-radius: var(--radius);
  box-shadow: 0 1px 3px rgba(0,0,0,.08);
  max-height: calc(100vh - 260px);
  max-width: 100%;
  --cell-doc-w: 104px;
  --cell-day-w: 36px;
  --cell-h: 38px;
  --cell-font-doc: 11.5px;
  --cell-font-day: 10px;
  --cell-font-turno: 10.5px;
}
.grid-wrap.zoom-s {
  --cell-doc-w: 84px;
  --cell-day-w: 26px;
  --cell-h: 30px;
  --cell-font-doc: 10px;
  --cell-font-day: 8.5px;
  --cell-font-turno: 9px;
}
.grid-wrap.zoom-l {
  --cell-doc-w: 128px;
  --cell-day-w: 48px;
  --cell-h: 48px;
  --cell-font-doc: 13.5px;
  --cell-font-day: 11px;
  --cell-font-turno: 13px;
}
.grid-wrap.zoom-xl {
  --cell-doc-w: 150px;
  --cell-day-w: 58px;
  --cell-h: 56px;
  --cell-font-doc: 15px;
  --cell-font-day: 12px;
  --cell-font-turno: 15px;
}
/* Vista "Turni": colonne fisse e larghe per contenere i nomi medico, indipendenti dallo zoom
   della vista "Medici" (va dopo le regole .zoom-* per avere sempre la precedenza quando attiva). */
.grid-wrap.vista-turni {
  --cell-doc-w: 76px;
  --cell-day-w: 96px;
  --cell-h: 40px;
  --cell-font-doc: 10.5px;
  --cell-font-day: 11px;
  --cell-font-turno: 12px;
}
.grid-cell-turno.turno-vuoto { color: #ccc; }
.grid { display: inline-block; min-width: 100%; }
.grid-row { display: flex; }
.grid-row.header { position: sticky; top: 0; z-index: 3; background: #fafbfc; }
.grid-cell-doc {
  position: sticky; left: 0; z-index: 2; background: #fff;
  width: var(--cell-doc-w); flex: 0 0 var(--cell-doc-w);
  padding: 0 8px; font-size: var(--cell-font-doc); font-weight: 700;
  display: flex; align-items: center;
  border-right: 1px solid var(--border); border-bottom: 1px solid var(--border);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  height: var(--cell-h);
  transition: width .15s, height .15s;
}
.grid-row.header .grid-cell-doc { z-index: 4; background: #fafbfc; font-size: 10px; color: var(--muted); }
.grid-cell-day {
  width: var(--cell-day-w); flex: 0 0 var(--cell-day-w); height: var(--cell-h);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  font-size: var(--cell-font-day); font-weight: 700; line-height: 1.2;
  border-right: 1px solid var(--border); border-bottom: 1px solid var(--border);
  color: var(--muted);
  transition: width .15s, height .15s;
}
.grid-cell-day.weekend, .grid-cell-day.festivo, .grid-cell-doc.weekend, .grid-cell-doc.festivo { background: #fff4e5; color: #b26a00; }
.grid-cell-day.oggi, .grid-cell-doc.oggi { background: #e8f0fe; color: var(--primary-dark); }
/* Violazione di regola (senza postazioni scoperte): solo bordo, meno grave. */
.grid-cell-day.problematico, .grid-cell-doc.problematico { box-shadow: inset 0 0 0 2px var(--danger); color: var(--danger); font-weight: 800; }
.grid-cell-day.avviso-ore, .grid-cell-doc.avviso-ore { box-shadow: inset 0 0 0 2px var(--warning); color: #b26a00; font-weight: 800; }
/* Postazione/e scoperta/e: errore piu' grave di una violazione, sfondo pieno per saltare subito
   all'occhio (vince su weekend/festivo/oggi e sulla sola violazione di regola). */
.grid-cell-day.scoperta, .grid-cell-doc.scoperta { background: var(--danger); color: #fff; font-weight: 800; box-shadow: none; }
.grid-cell-turno {
  width: var(--cell-day-w); flex: 0 0 var(--cell-day-w); height: var(--cell-h);
  display: flex; align-items: center; justify-content: center;
  font-size: var(--cell-font-turno); font-weight: 700;
  border-right: 1px solid #eee; border-bottom: 1px solid #eee;
  position: relative;
  transition: width .15s, height .15s;
}
.t-mattino { background: #e3f2fd; color: #0d47a1; }
.t-pomeriggio { background: #fff3e0; color: #b25c00; }
.t-notte { background: #311b92; color: #fff; }
.t-lungo { background: #e0f2f1; color: #00695c; }
.t-riposo { background: #e8f5e9; color: #2e7d32; }
.t-assenza { background: #eceff1; color: #546e7a; }
.t-altro { background: #f3e5f5; color: #6a1b9a; }
.t-empty { background: #fafafa; color: #ccc; border-style: dashed; }
.grid-cell-turno.violazione { box-shadow: inset 0 0 0 2px var(--danger); }
.grid-cell-turno.violazione::after {
  content: ''; position: absolute; top: 2px; right: 2px;
  width: 6px; height: 6px; border-radius: 50%; background: var(--danger);
}
.legend-row { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; font-size: 13px; }
.legend-swatch { width: 18px; height: 18px; border-radius: 4px; flex: 0 0 auto; }

.placeholder {
  text-align: center; color: var(--muted);
  margin-top: 60px; font-size: 15px; line-height: 1.6;
}

/* ---- FAB ---- */
.fab {
  position: fixed; right: 20px; bottom: calc(96px + var(--safe-bottom));
  width: 54px; height: 54px; border-radius: 50%;
  background: var(--primary); color: #fff; border: none;
  font-size: 26px; box-shadow: 0 4px 12px rgba(0,0,0,.3);
  z-index: 15;
}

/* ---- Bottom sheet ---- */
/* z-index sopra al modal (49/50): il foglio puo' aprirsi SOPRA un modal gia' aperto (es. dal
   dettaglio giorno si apre il foglio "libera postazione"/"assegna medico" senza chiudere il modal
   sottostante, per poterlo poi aggiornare al suo posto). */
.sheet-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.4);
  z-index: 59; opacity: 0; pointer-events: none; transition: opacity .2s;
}
.sheet-overlay.open { opacity: 1; pointer-events: auto; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 60;
  background: #fff;
  border-radius: 18px 18px 0 0;
  max-height: 86vh;
  overflow-y: auto;
  transform: translateY(100%);
  transition: transform .25s ease;
  padding-bottom: calc(18px + var(--safe-bottom));
}
.sheet.open { transform: translateY(0); }
.sheet-handle { width: 40px; height: 4px; background: #d8dadd; border-radius: 4px; margin: 10px auto; }
.sheet-content { padding: 4px 20px 10px; }
.sheet-content h3 { margin-top: 4px; }

.form-card label, .sheet-content label {
  display: block; font-size: 13px; font-weight: 600; color: var(--muted); margin-bottom: 12px;
}
.form-card input, .form-card select,
.sheet-content input, .sheet-content select {
  display: block; width: 100%; margin-top: 6px; padding: 11px;
  font-size: 15px; border: 1px solid var(--border); border-radius: 10px; background: #f8f9fa;
}
.form-card { background: #fff; border-radius: var(--radius); padding: 18px; box-shadow: 0 1px 3px rgba(0,0,0,.08); }
.readonly-field { background: #eee !important; color: var(--muted); }
.field-row { display: flex; gap: 10px; flex-wrap: wrap; }
.field-row > label { flex: 1 1 120px; min-width: 0; }
.sheet-actions { display: flex; gap: 10px; margin-top: 8px; }
.sheet-actions .btn { flex: 1; }
.checkbox-row { display: flex; align-items: center; gap: 8px; font-size: 14px; color: var(--text); margin-bottom: 14px; }
.checkbox-row input { width: auto; margin: 0; }

/* ---- Modal centrato (menu assegnazione turno) ---- */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.45);
  z-index: 49; opacity: 0; pointer-events: none; transition: opacity .15s;
}
.modal-overlay.open { opacity: 1; pointer-events: auto; }
.modal {
  position: fixed; top: 50%; left: 50%;
  width: 92%; max-width: 380px; max-height: 82vh;
  background: #fff; border-radius: 16px;
  z-index: 50; overflow: hidden;
  display: flex; flex-direction: column;
  opacity: 0; pointer-events: none;
  transform: translate(-50%, -48%);
  transition: opacity .15s, transform .15s;
  box-shadow: 0 12px 34px rgba(0,0,0,.3);
}
.modal.open { opacity: 1; pointer-events: auto; transform: translate(-50%, -50%); }
.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 8px 12px 16px; border-bottom: 1px solid var(--border); flex: 0 0 auto;
}
.modal-header h3 { margin: 0; font-size: 15px; }
.modal-content { overflow-y: auto; flex: 1 1 auto; }

.turno-menu-header {
  padding: 9px 16px 6px; font-size: 11px; font-weight: 700; color: var(--muted);
  background: #fafbfc; text-transform: uppercase; letter-spacing: .02em;
  border-top: 1px solid var(--border); border-bottom: 1px solid var(--border);
}
.turno-menu-item {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 12px 16px; font-size: 14.5px; border-bottom: 1px solid #f1f2f4;
}
.turno-menu-item:active { background: #f1f2f4; }
.turno-menu-item.disabled { color: var(--muted); font-style: italic; }
.turno-menu-item .tmi-sub { font-size: 12px; color: var(--muted); margin-left: 6px; }
.turno-menu-badge { width: 9px; height: 9px; border-radius: 50%; background: var(--danger); flex: 0 0 auto; }
.turno-menu-item.danger { color: var(--danger); font-weight: 700; }
.turno-menu-item.scoperta { background: #fdecea; color: var(--danger); }

/* ---- Log Viewer ---- */
.log-level-select {
  padding: 7px 8px; border: 1px solid var(--border); border-radius: 8px;
  font-size: 13px; background: #f8f9fa; flex: 0 0 auto;
}
.log-content {
  background: #1e1e1e; color: #fff; border-radius: var(--radius);
  font-family: 'Courier New', monospace; font-size: 11.5px;
  padding: 10px; overflow-y: auto;
  max-height: calc(100vh - 260px);
}
.log-entry { margin-bottom: 3px; padding: 2px 6px; line-height: 1.4; word-break: break-word; }
.log-entry .log-time { color: #888; }
.log-entry .log-badge { padding: 1px 6px; border-radius: 3px; font-size: 9.5px; margin-right: 6px; color: #fff; font-weight: 700; }
.log-status-bar { font-size: 11px; color: var(--muted); text-align: center; margin-top: 8px; }

/* ---- Toast ---- */
.toast {
  position: fixed; left: 50%; bottom: calc(100px + var(--safe-bottom));
  transform: translate(-50%, 20px);
  background: #212529; color: #fff;
  padding: 11px 18px; border-radius: 10px;
  font-size: 14px; z-index: 70;
  opacity: 0; pointer-events: none;
  transition: opacity .2s, transform .2s;
  max-width: 85vw; text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: var(--danger); }
.toast.success { background: var(--success); }

/* ---- Loading ---- */
.loading-overlay {
  position: fixed; inset: 0; background: rgba(255,255,255,.6);
  display: flex; align-items: center; justify-content: center; z-index: 80;
}
.spinner {
  width: 38px; height: 38px; border-radius: 50%;
  border: 4px solid #d6d6d6; border-top-color: var(--primary);
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---- Small screens tweak ---- */
@media (max-width: 360px) {
  .card-title { font-size: 14px; }
  .topbar-title { font-size: 15px; }
}
