/* ===== RESET & BASE ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #0f172a;
  --surface: #1e293b;
  --surface2: #273549;
  --border: #334155;
  --accent: #3b82f6;
  --accent2: #0ea5e9;
  --danger: #ef4444;
  --warn: #f59e0b;
  --success: #22c55e;
  --text: #f1f5f9;
  --text2: #94a3b8;
  --radius: 8px;
  --shadow: 0 4px 24px rgba(0,0,0,0.4);
}

html, body {
  height: 100%;
  font-family: 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  overflow-x: hidden;
}

/* ===== HEADER ===== */
header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 12px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0;
  z-index: 100;
  gap: 8px;
}

header h1 {
  font-size: 18px;
  font-weight: 700;
  color: var(--accent2);
  white-space: nowrap;
}

.header-actions {
  display: flex;
  gap: 6px;
  align-items: center;
  flex-wrap: wrap;
}

/* ===== NAV TABS ===== */
.tabs {
  display: flex;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0 8px;
  overflow-x: auto;
  scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }

.tab-btn {
  padding: 10px 16px;
  background: none;
  border: none;
  border-bottom: 3px solid transparent;
  color: var(--text2);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.2s;
}
.tab-btn.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}
.tab-btn:hover:not(.active) { color: var(--text); }

/* ===== MAIN CONTENT ===== */
main {
  padding: 16px;
  max-width: 100%;
  overflow-x: auto;
}

.view { display: none; }
.view.active { display: block; }

/* ===== BUTTONS ===== */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 7px 14px;
  border: none;
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s;
  white-space: nowrap;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: #2563eb; }
.btn-success { background: var(--success); color: #fff; }
.btn-success:hover { background: #16a34a; }
.btn-warn { background: var(--warn); color: #000; }
.btn-warn:hover { background: #d97706; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { background: #dc2626; }
.btn-ghost {
  background: var(--surface2);
  color: var(--text);
  border: 1px solid var(--border);
}
.btn-ghost:hover { background: var(--border); }
.btn-sm { padding: 4px 10px; font-size: 12px; }
.btn-icon { padding: 6px 8px; }

/* ===== FORMS ===== */
.form-group {
  margin-bottom: 12px;
}
.form-group label {
  display: block;
  margin-bottom: 4px;
  font-size: 12px;
  color: var(--text2);
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
input[type="text"],
input[type="time"],
input[type="color"],
select,
textarea {
  width: 100%;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  padding: 8px 10px;
  font-size: 14px;
  outline: none;
  transition: border-color 0.15s;
}
input:focus, select:focus, textarea:focus {
  border-color: var(--accent);
}
input[type="color"] {
  padding: 4px;
  height: 36px;
  cursor: pointer;
}

/* ===== CARDS ===== */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
}
.card-title {
  font-size: 14px;
  font-weight: 600;
  color: var(--text2);
  margin-bottom: 12px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

/* ===== LIST ITEMS ===== */
.list-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 6px;
}
.list-item .item-name {
  flex: 1;
  font-weight: 500;
}
.list-item .item-color {
  width: 14px;
  height: 14px;
  border-radius: 3px;
  flex-shrink: 0;
}

/* ===== WEEK NAV ===== */
.week-nav {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}
.week-label {
  font-size: 15px;
  font-weight: 600;
  color: var(--text);
  flex: 1;
  text-align: center;
}

/* ===== CALENDAR TABLE ===== */
.calendar-wrapper {
  overflow-x: auto;
  border-radius: var(--radius);
  border: 1px solid var(--border);
}

.calendar-table {
  width: 100%;
  min-width: 700px;
  border-collapse: collapse;
  background: var(--surface);
}

.calendar-table th {
  background: var(--surface2);
  padding: 8px 6px;
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text2);
  border: 1px solid var(--border);
  text-align: center;
  white-space: nowrap;
}

.calendar-table th.col-worker {
  background: #1a2744;
  color: var(--text);
  min-width: 110px;
  text-align: left;
  padding-left: 10px;
}

.calendar-table th.today-col {
  background: #1e3a5f;
  color: var(--accent2);
}

.calendar-table td {
  border: 1px solid var(--border);
  vertical-align: top;
  padding: 0;
  min-width: 90px;
}

.calendar-table td.worker-name-cell {
  padding: 8px 10px;
  font-weight: 600;
  font-size: 14px;
  background: var(--surface);
  position: sticky;
  left: 0;
  z-index: 10;
  border-right: 2px solid var(--accent);
  word-break: break-word;
  max-width: 130px;
}

.calendar-table td.today-col {
  background: rgba(59, 130, 246, 0.05);
}

/* ===== SHIFT CELL ===== */
.shift-cell {
  min-height: 60px;
  padding: 4px;
  cursor: pointer;
  transition: background 0.15s;
  position: relative;
}
.shift-cell:hover { background: rgba(255,255,255,0.03); }

.shift-badge {
  display: block;
  border-radius: 5px;
  padding: 4px 6px;
  margin-bottom: 3px;
  font-size: 12px;
  font-weight: 600;
  line-height: 1.3;
  cursor: pointer;
  transition: opacity 0.15s;
}
.shift-badge:hover { opacity: 0.8; }
.badge-dept-line { display: block; }
.badge-dept-line + .badge-dept-line { margin-top: 1px; }
.shift-badge .badge-time { display: block; font-weight: 400; opacity: 0.85; font-size: 11px; margin-top: 2px; }
.shift-add-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--surface2);
  border: 1px dashed var(--border);
  color: var(--text2);
  font-size: 18px;
  cursor: pointer;
  margin: 2px auto;
  transition: all 0.15s;
  line-height: 1;
}
.shift-add-btn:hover {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

/* ===== HOURS SUMMARY ===== */
.hours-cell {
  padding: 8px 10px;
  text-align: center;
  font-weight: 700;
  font-size: 13px;
  background: var(--surface2);
  white-space: nowrap;
}
.hours-cell.over { color: var(--warn); }
.hours-cell.ok { color: var(--success); }

/* ===== ALERT BANNER ===== */
.alert-banner {
  background: rgba(239,68,68,0.1);
  border: 1px solid var(--danger);
  border-radius: var(--radius);
  padding: 10px 14px;
  margin-bottom: 14px;
  font-size: 13px;
  color: #fca5a5;
  display: none;
}
.alert-banner.visible { display: block; }
.alert-banner strong { color: var(--danger); }

/* ===== MODAL ===== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  z-index: 200;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.modal-overlay.open { display: flex; }

.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 20px;
  width: 100%;
  max-width: 400px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: var(--shadow);
}
.modal-title {
  font-size: 16px;
  font-weight: 700;
  margin-bottom: 16px;
  color: var(--text);
}
.modal-actions {
  display: flex;
  gap: 8px;
  margin-top: 16px;
  justify-content: flex-end;
}

/* ===== DEPT CHIP ===== */
.dept-chip {
  display: inline-flex;
  align-items: center;
  padding: 6px 12px;
  border-radius: 6px;
  border: 2px solid transparent;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  transition: all 0.15s;
  margin: 3px;
  user-select: none;
  opacity: 0.55;
}
.dept-chip.selected {
  border-color: currentColor;
  opacity: 1;
  box-shadow: 0 0 0 2px currentColor inset;
}

/* ===== COVERAGE INDICATORS ===== */
.day-header-wrapper {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

/* ===== COVERAGE TFOOT ===== */
tfoot td {
  vertical-align: middle !important;
}
.coverage-foot-label {
  padding: 6px 10px;
  font-size: 12px;
  font-weight: 700;
  color: var(--danger);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  background: var(--surface);
  border-top: 1px solid rgba(239,68,68,0.3);
  position: sticky;
  left: 0;
  z-index: 10;
}
.coverage-foot-cell {
  padding: 4px 6px;
  border-top: 1px solid rgba(239,68,68,0.3);
  vertical-align: top;
}
.coverage-foot-cell.today-col {
  background: rgba(239,68,68,0.08);
}
.coverage-missing-item {
  display: block;
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
  line-height: 2;
}

/* ===== TIME PICKERS GRID ===== */
.time-pickers-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
}
@media (min-width: 480px) {
  .time-pickers-grid { grid-template-columns: 1fr 1fr; }
}

/* ===== TIME PICKER ===== */
.tp-activate {
  width: 100%;
  padding: 9px;
  background: var(--surface2);
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  color: var(--text2);
  font-size: 13px;
  cursor: pointer;
  transition: all 0.15s;
}
.tp-activate:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.tp-row {
  display: flex;
  align-items: center;
  gap: 4px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 4px 6px;
}

.tp-arr {
  width: 32px;
  height: 32px;
  border: none;
  border-radius: 6px;
  background: var(--border);
  color: var(--text);
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.1s;
}
.tp-arr:hover { background: var(--accent); }

.tp-h {
  font-size: 22px;
  font-weight: 700;
  min-width: 32px;
  text-align: center;
  color: var(--text);
  letter-spacing: 1px;
}

.tp-col {
  font-size: 22px;
  font-weight: 700;
  color: var(--text2);
  margin: 0 1px;
}

.tp-mins {
  display: flex;
  gap: 3px;
  flex: 1;
}

.tp-m {
  flex: 1;
  height: 32px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text2);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.1s;
}
.tp-m:hover { border-color: var(--accent); color: var(--accent); }
.tp-m.sel {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.tp-clear {
  width: 28px;
  height: 28px;
  border: none;
  border-radius: 50%;
  background: none;
  color: var(--text2);
  font-size: 16px;
  cursor: pointer;
  flex-shrink: 0;
  line-height: 1;
  transition: all 0.1s;
}
.tp-clear:hover { color: var(--danger); }

/* ===== SETTINGS LAYOUT ===== */
.settings-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}
@media (max-width: 600px) {
  .settings-grid { grid-template-columns: 1fr; }
}

.add-form {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
  align-items: flex-end;
}
.add-form .form-group { flex: 1; margin: 0; }

/* ===== HOURS VIEW ===== */
.hours-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 12px;
}
.hours-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
}
.hours-card .worker-name {
  font-weight: 700;
  font-size: 15px;
  margin-bottom: 8px;
}
.hours-card .total-hours {
  font-size: 28px;
  font-weight: 800;
  color: var(--accent2);
}
.hours-card .hours-label {
  font-size: 11px;
  color: var(--text2);
}
.hours-day-row {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  padding: 2px 0;
  border-bottom: 1px solid var(--border);
  color: var(--text2);
}
.hours-day-row:last-child { border: none; }
.hours-day-row span:last-child { color: var(--text); font-weight: 500; }

#print-tables { display: none; }

/* ===== EMPTY STATE ===== */
.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: var(--text2);
}
.empty-state .icon { font-size: 40px; margin-bottom: 8px; }
.empty-state p { font-size: 14px; }

/* ===== PRINT STYLES ===== */
@media print {
  @page {
    size: A4 landscape;
    margin: 8mm 10mm;
  }

  * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }

  /* Rimuovi vincoli di altezza/overflow per stampa multi-pagina */
  html, body {
    height: auto !important;
    overflow: visible !important;
    background: #fff !important;
  }

  body {
    color: #000 !important;
    font-size: 16px !important;
    font-family: 'Segoe UI', system-ui, sans-serif !important;
  }

  ::-webkit-scrollbar { display: none !important; }

  header, .tabs, .week-nav .btn,
  .alert-banner, .print-hide {
    display: none !important;
  }

  tfoot { display: none !important; }

  .view { display: block !important; }
  .view:not(#view-calendar) { display: none !important; }

  main, #view-calendar, #calendar-table-wrapper {
    padding: 0 !important;
    overflow: visible !important;
    height: auto !important;
    background: transparent !important;
  }

  .week-nav {
    justify-content: center;
    margin-bottom: 10px;
  }
  .week-label {
    font-size: 17px !important;
    font-weight: 700 !important;
    color: #000 !important;
  }

  .calendar-wrapper {
    overflow: visible !important;
    height: auto !important;
    border: none !important;
    background: transparent !important;
  }

  .calendar-table {
    min-width: unset !important;
    width: 100% !important;
    background: transparent !important;
    border-collapse: collapse !important;
    table-layout: fixed !important;
  }

  /* Intestazione ripetuta su ogni pagina */
  .calendar-table thead {
    display: table-header-group !important;
  }

  .calendar-table th {
    background: #e2e8f0 !important;
    color: #000 !important;
    border: 1px solid #94a3b8 !important;
    padding: 8px 5px !important;
    font-size: 16px !important;
    font-weight: 700 !important;
    text-align: center !important;
  }
  .calendar-table th.col-worker {
    background: #cbd5e1 !important;
    text-align: left !important;
    padding-left: 8px !important;
  }
  .calendar-table th.today-col {
    background: #bfdbfe !important;
  }

  /* Nascondi tabella interattiva, mostra tabelle di stampa */
  #calendar-table-wrapper { display: none !important; }
  #print-tables { display: block !important; }

  /* Ogni pagina di stampa */
  .print-page {
    height: 80vh;
    width: 100%;
    display: flex;
    flex-direction: column;
    page-break-after: always;
    break-after: page;
    box-sizing: border-box;
  }
  .print-page:last-child {
    page-break-after: avoid;
    break-after: avoid;
  }

  /* Etichetta settimana */
  .print-week-label {
    font-size: 15px;
    font-weight: 700;
    text-align: center;
    margin-bottom: 6px;
    flex-shrink: 0;
  }

  /* Tabella occupa tutto lo spazio rimanente nella pagina */
  .print-table {
    flex: 1;
    height: 100%;
  }

  /* Le righe si distribuiscono proporzionalmente */
  .print-table tbody tr {
    height: auto !important;
  }

  /* Sfondi in stampa: grigio per intestazione e nome lavoratore, trasparente per le celle */
  .calendar-table td,
  .calendar-table td.today-col,
  .shift-cell {
    background: transparent !important;
  }
  .calendar-table th {
    background: #e2e8f0 !important;
  }
  .calendar-table td.worker-name-cell {
    background: #f1f5f9 !important;
  }

  /* Bordi neri tra righe e colonne */
  .calendar-table th,
  .calendar-table td {
    border: 1.5px solid #000 !important;
  }

  /* Celle */
  .calendar-table td {
    background: transparent !important;
    vertical-align: middle !important;
  }

  .calendar-table td.worker-name-cell {
    background: #f1f5f9 !important;
    color: #000 !important;
    font-size: 16px !important;
    font-weight: 700 !important;
    border-right: 3px solid #000 !important;
    position: static !important;
    word-break: break-word !important;
    max-width: none !important;
    padding: 8px !important;
  }

  .calendar-table td.today-col {
    background: #eff6ff !important;
  }

  /* Celle turni: centrate, senza sfondo né bordo badge */
  .shift-cell {
    min-height: 50px !important;
    padding: 6px 4px !important;
    cursor: default !important;
    text-align: center !important;
    background: transparent !important;
  }

  .shift-badge {
    display: block !important;
    background: transparent !important;
    border: none !important;
    border-radius: 0 !important;
    padding: 0 !important;
    margin: 0 0 4px !important;
    color: #000 !important;
    text-align: center !important;
  }

  .badge-dept-line {
    display: block !important;
    font-size: 15px !important;
    font-weight: 700 !important;
    line-height: 1.3 !important;
  }
  .shift-badge .badge-time {
    font-size: 13px !important;
    font-weight: 400 !important;
    color: #475569 !important;
    opacity: 1 !important;
  }

  .shift-badge + .shift-badge {
    border-top: 1px dashed #cbd5e1 !important;
    padding-top: 4px !important;
  }

  .shift-add-btn { display: none !important; }
}
