:root {
  --bg: #f4f6f9;
  --panel: #ffffff;
  --border: #dfe4ea;
  --text: #1f2933;
  --muted: #7b8794;
  --accent: #2563eb;
  --accent-dark: #1d4ed8;
  --danger: #dc2626;
  --shift: #e0edff;
  --shift-border: #93c0ff;
  --today: #fff7e6;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
  background: var(--bg);
}

.hidden { display: none !important; }
.muted { color: var(--muted); }
.center { text-align: center; }

button {
  font: inherit;
  cursor: pointer;
  border: none;
  border-radius: 6px;
  padding: 8px 14px;
  background: var(--accent);
  color: #fff;
}
button:hover { background: var(--accent-dark); }
button.secondary { background: #e5e9f0; color: var(--text); }
button.secondary:hover { background: #d5dbe6; }
button.danger { background: var(--danger); }

input, textarea {
  font: inherit;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  width: 100%;
  background: #fff;
  color: var(--text);
}
textarea { resize: vertical; }

/* --- Login --- */
.login-wrap {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 20px;
}
.login-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 32px;
  width: 100%;
  max-width: 360px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.06);
}
.login-logo { width: 160px; height: auto; display: block; margin: 0 auto 16px; }
.login-card h1 { margin: 0 0 4px; }
.login-card label {
  display: block;
  margin: 16px 0 0;
  font-weight: 600;
  font-size: 14px;
}
.login-card input { margin-top: 6px; font-weight: 400; }
.login-card button { width: 100%; margin-top: 20px; }
.error { color: var(--danger); font-size: 14px; }

/* --- Top bar --- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 20px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.brand { font-weight: 700; font-size: 18px; display: flex; align-items: center; gap: 8px; }
.brand-logo { height: 22px; width: auto; display: block; }
.week-nav { display: flex; align-items: center; gap: 10px; }
.week-nav button { padding: 6px 12px; }
#week-label { font-weight: 600; min-width: 190px; text-align: center; }
.user-box { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.filters-group {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 6px 12px 6px 10px;
  border: 2px solid var(--accent);
  border-radius: 10px;
  background: #eef3ff;
  flex-wrap: wrap;
}
.filters-heading {
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--accent-dark);
}
.filter {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
}
.filters-group select {
  border-color: var(--accent);
  font-weight: 600;
}
.clear-filters {
  padding: 6px 12px;
  font-size: 13px;
  font-weight: 700;
  background: #fff;
  color: var(--accent-dark);
  border: 1px solid var(--accent);
}
.clear-filters:hover { background: var(--accent); color: #fff; }
select {
  font: inherit;
  padding: 7px 9px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fff;
  color: var(--text);
}

main { padding: 20px; }
.toolbar { display: flex; align-items: center; gap: 12px; margin-bottom: 14px; flex-wrap: wrap; }
.add-staff {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
}
.add-staff input { width: 220px; font-weight: 400; }

/* --- Rota sections (Dayshift / Backshift / Nightshift) --- */
.rota-section { margin-bottom: 26px; }
.section-head {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 8px;
  flex-wrap: wrap;
}
.section-head h2 {
  margin: 0;
  font-size: 16px;
  font-weight: 700;
}
.section-add { font-weight: 400; color: var(--text); }
.section-add input { width: 200px; }
.section-empty { margin: 6px 2px 0; font-size: 13px; }

/* Per-section accent colour, to tell the three sections apart at a glance. */
#block-day { --sec: #d97706; }   /* amber  — daytime */
#block-back { --sec: #0d9488; }  /* teal   — back/evening */
#block-night { --sec: #4338ca; } /* indigo — nights */
.rota-section .section-head h2 {
  border-left: 5px solid var(--sec);
  padding-left: 10px;
  color: var(--sec);
}
.rota-section .rota-grid { border-top: 3px solid var(--sec); }
#block-day .rota-grid thead th { background: #fdf3e6; }
#block-back .rota-grid thead th { background: #e7f6f4; }
#block-night .rota-grid thead th { background: #e7e8f7; }
/* Keep the "today" column highlight winning over the section tint. */
.rota-section .rota-grid thead th.today { background: var(--today); }

/* Section checkboxes in the edit dialog. */
.sections-field {
  border: 1px solid var(--border);
  border-radius: 6px;
  margin-top: 14px;
  padding: 8px 12px 12px;
}
.sections-field legend { font-size: 13px; font-weight: 600; padding: 0 6px; }
.sections-field .chk {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-right: 14px;
  font-weight: 400;
}
.sections-field .chk input { width: auto; }

/* --- Grid --- */
/* Fixed layout with explicit column widths so all three section tables line up
   column-for-column no matter what each one contains. */
.grid-scroll { overflow-x: auto; }
.rota-grid {
  border-collapse: collapse;
  table-layout: fixed;
  width: 100%;
  min-width: 1000px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
}
.rota-grid th, .rota-grid td {
  border: 1px solid var(--border);
  padding: 8px;
  vertical-align: top;
}
/* Stronger vertical dividers so each day column is clearly separated. */
.rota-grid th + th, .rota-grid td + td {
  border-left: 2px solid #aab4c4;
}
.rota-grid thead th {
  background: #f0f3f8;
  font-size: 13px;
  text-align: center;
  position: sticky;
  top: 0;
}
.rota-grid thead th.today { background: var(--today); }
.staff-name {
  width: 150px;
  font-weight: 600;
  word-break: break-word;
  background: #fafbfc;
}
.staff-name.editable-name { cursor: pointer; }
.staff-name.editable-name:hover { background: #eef3fb; text-decoration: underline; }
.col-total, .cell-total { width: 74px; }
.col-total { text-align: right; }
.cell-total {
  text-align: right;
  font-weight: 600;
  white-space: nowrap;
  background: #fafbfc;
  color: var(--text);
}
.day-cell { height: 70px; }
.day-cell.editable { cursor: pointer; }
/* Per-day "staff on shift" count row at the bottom of each section. */
.section-footer td {
  background: #eef1f6;
  font-weight: 600;
  vertical-align: middle;
}
.section-footer .footer-label { font-size: 13px; color: var(--muted); }
.section-footer .footer-count { text-align: center; }
.section-footer .footer-count.zero { color: var(--muted); font-weight: 400; }
.day-cell.editable:hover { background: #f7faff; }
.day-cell.today { background: var(--today); }

/* --- Matrix layout: day columns × section bands --- */
/* Real gaps between columns so each day is a clearly separated lane. */
.rota-grid.matrix {
  border-collapse: separate;
  border-spacing: 10px 14px;
  border: none;
  border-radius: 0;
  overflow: visible;
  min-width: 1040px;
}
.rota-grid.matrix th,
.rota-grid.matrix td {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding-left: 14px;
  padding-right: 14px;
}
/* Gap now separates the days, so drop the heavy vertical divider here. */
.rota-grid.matrix th + th,
.rota-grid.matrix td + td { border-left: 1px solid var(--border); }
/* Wider left column for the section titles. */
.matrix thead .section-col { background: #f0f3f8; width: 150px; }
.section-row .section-col {
  width: 150px;
  font-size: 15px;
  font-weight: 700;
  vertical-align: middle;
  background: #fafbfc;
  color: var(--sec, var(--text));
  border-left: 5px solid var(--sec, var(--border));
}
.section-row.sec-day { --sec: #d97706; }
.section-row.sec-back { --sec: #0d9488; }
.section-row.sec-night { --sec: #4338ca; }
.matrix-cell { vertical-align: top; }
.matrix-cell.today { background: var(--today); }

.entry {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 2px 8px;
  border-radius: 6px;
  padding: 4px 8px;
  margin-bottom: 4px;
  font-size: 13px;
  line-height: 1.3;
}
.entry.clickable { cursor: pointer; }
.entry.clickable:hover { filter: brightness(0.97); }
.entry.cover .entry-name { color: var(--danger); font-weight: 800; }

/* "Cover needed" toggle in the shift dialog */
.cover-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 12px;
  font-weight: 600;
}
.cover-toggle input { width: auto; }
.entry-name { font-weight: 700; }
.entry-time { font-variant-numeric: tabular-nums; white-space: nowrap; }
.entry-co, .entry-note { flex-basis: 100%; font-size: 12px; color: var(--muted); }

.cell-count { font-size: 11px; color: var(--muted); margin-top: 2px; }
.cell-add {
  margin-top: 4px;
  padding: 2px 8px;
  font-size: 12px;
  background: transparent;
  color: var(--accent);
  border: 1px dashed var(--border);
  border-radius: 6px;
}
.cell-add:hover { background: #eef3fb; border-color: var(--accent); }

/* --- Weekly hours summary --- */
.hours-summary {
  margin-top: 22px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px 18px;
  max-width: 420px;
}
.hours-summary h3 { margin: 0 0 10px; font-size: 15px; }
.hours-list { display: grid; grid-template-columns: 1fr auto; gap: 5px 18px; }
.hours-row { display: contents; }
.hours-val { text-align: right; font-weight: 600; font-variant-numeric: tabular-nums; }

/* --- Mobile: stacked day-by-day cards --- */
.day-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  margin-bottom: 12px;
  overflow: hidden;
}
.day-card.today { box-shadow: 0 0 0 2px #eab308; }
.day-card-head {
  background: #f0f3f8;
  padding: 10px 14px;
  font-weight: 700;
}
.day-card.today .day-card-head { background: var(--today); }
.m-section { padding: 10px 14px; margin-top: 12px; border-top: 1px solid var(--border); }
.m-section:first-of-type { margin-top: 0; }
.m-section.sec-day { --sec: #d97706; }
.m-section.sec-back { --sec: #0d9488; }
.m-section.sec-night { --sec: #4338ca; }
.m-section-head {
  font-weight: 700;
  font-size: 13px;
  color: var(--sec, var(--text));
  border-left: 4px solid var(--sec, var(--border));
  padding-left: 8px;
  margin-bottom: 8px;
}
.m-empty { font-size: 13px; padding: 2px 0; }
.m-none { padding: 12px 14px; }
.day-card .entry { font-size: 14px; padding: 8px 10px; }

@media (max-width: 640px) {
  main { padding: 12px; }
  .topbar {
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
    padding: 12px;
  }
  .week-nav { justify-content: space-between; }
  #week-label { min-width: 0; flex: 1; }
  .user-box { justify-content: flex-start; }
  .toolbar { margin-bottom: 12px; }
}

.shift-chip {
  display: block;
  background: var(--shift);
  border: 1px solid var(--shift-border);
  border-radius: 6px;
  padding: 4px 6px;
  margin-bottom: 4px;
  font-size: 13px;
  line-height: 1.3;
}
.shift-chip .time { font-weight: 600; }
.shift-chip .company { font-size: 12px; font-weight: 600; }
.shift-chip .note { color: var(--muted); font-size: 12px; }

/* --- Colour swatches --- */
.swatches { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 6px; }
.swatch {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  padding: 0;
  border: 2px solid transparent;
  box-shadow: 0 0 0 1px var(--border);
}
.swatch:hover { transform: scale(1.08); }
.swatch.selected {
  border-color: #fff;
  box-shadow: 0 0 0 2px var(--text);
}

/* --- Admin screen --- */
.admin-main { max-width: 820px; margin: 0 auto; }
.admin-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 18px 20px;
  margin-bottom: 20px;
}
.admin-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}
.admin-head h2 { margin: 0; font-size: 18px; }
.admin-table { width: 100%; border-collapse: collapse; }
.admin-table th, .admin-table td {
  text-align: left;
  padding: 10px 8px;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
}
.admin-table th { color: var(--muted); font-size: 12px; text-transform: uppercase; }
.admin-actions { display: flex; gap: 8px; justify-content: flex-end; }
button.small { padding: 5px 10px; font-size: 13px; }
.inline-add { display: flex; gap: 8px; align-items: center; }
.inline-add input { width: 200px; }
.admin-note { margin: 0 0 12px; font-size: 13px; }
.toolbar-export { display: flex; gap: 8px; flex-wrap: wrap; }

/* --- Dialog --- */
dialog {
  border: none;
  border-radius: 12px;
  padding: 24px;
  width: 90%;
  max-width: 380px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.2);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.35); }
dialog h3 { margin: 0 0 4px; }
dialog label {
  display: block;
  margin-top: 14px;
  font-weight: 600;
  font-size: 14px;
}
dialog input { margin-top: 6px; font-weight: 400; }
dialog .row { display: flex; gap: 12px; }
dialog .row label { flex: 1; }
.dialog-actions { display: flex; align-items: center; gap: 10px; margin-top: 22px; }
.dialog-actions .spacer { flex: 1; }

/* --- Custom person suggestions (replaces native datalist) --- */
.input-suggest { position: relative; display: block; }
.suggest {
  position: absolute;
  left: 0; right: 0; top: 100%;
  z-index: 20;
  margin-top: 4px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 8px 22px rgba(0, 0, 0, 0.14);
  max-height: 200px;
  overflow-y: auto;
}
.suggest.hidden { display: none; }
.suggest-item { padding: 9px 12px; cursor: pointer; font-size: 14px; }
.suggest-item:hover { background: #eef3fb; }

/* type="search" is used to stop Safari's Contacts autofill; strip its native
   search chrome so these fields still look like normal inputs. */
input[type="search"] { -webkit-appearance: none; appearance: none; }
input[type="search"]::-webkit-search-decoration,
input[type="search"]::-webkit-search-cancel-button { -webkit-appearance: none; display: none; }

/* Staff-first login: manager toggle link */
.login-toggle { text-align: center; margin-top: 16px; font-size: 13px; }
.login-toggle a { color: var(--accent); cursor: pointer; text-decoration: none; }
.login-toggle a:hover { text-decoration: underline; }

/* ===================================================================== */
/* Monthly overview                                                       */
/* ===================================================================== */
.month-grid {
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  gap: 6px;
}

/* Weekday header cells */
.month-dow {
  text-align: center;
  font-size: 12px;
  font-weight: 700;
  color: var(--muted);
  padding: 4px 0 2px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

/* A single day */
.month-cell {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 4px;
  min-height: 92px;
  padding: 6px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  text-align: left;
  font: inherit;
  color: var(--text);
  cursor: pointer;
  transition: box-shadow 0.12s, border-color 0.12s, transform 0.02s;
}
.month-cell:hover {
  border-color: var(--accent);
  box-shadow: 0 2px 8px rgba(37, 99, 235, 0.12);
}
.month-cell:active { transform: translateY(1px); }
.month-cell.other-month { background: #fafbfc; }
.month-cell.other-month .month-daynum { color: var(--muted); }
.month-cell.today { border-color: #eab308; box-shadow: 0 0 0 2px #fde68a; }
.month-cell.today .month-daynum { color: #b45309; }

.month-cell-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.month-daynum { font-weight: 700; font-size: 14px; }
.month-cover { color: var(--danger); font-size: 11px; line-height: 1; }

/* Per-section count chips */
.month-badges { display: flex; flex-wrap: wrap; gap: 4px; }
.month-badge {
  --sec: var(--accent);
  min-width: 20px;
  padding: 1px 6px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 700;
  color: #fff;
  text-align: center;
  background: var(--sec);
}
.month-badge.sec-day { --sec: #d97706; }
.month-badge.sec-back { --sec: #0d9488; }
.month-badge.sec-night { --sec: #4338ca; }

.month-cell-foot {
  margin-top: auto;
  font-size: 11px;
  color: var(--muted);
}

/* Legend under the grid */
.month-legend {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 14px;
  margin-top: 14px;
  font-size: 13px;
}
.ml-item { display: inline-flex; align-items: center; gap: 6px; }
.ml-dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.ml-hint { margin-left: auto; }

/* Phones: tighter grid, hide the "on shift" footnote to save space */
@media (max-width: 720px) {
  .month-grid { gap: 3px; }
  .month-cell { min-height: 64px; padding: 4px; border-radius: 6px; gap: 3px; }
  .month-daynum { font-size: 13px; }
  .month-badge { min-width: 18px; padding: 0 5px; font-size: 11px; }
  .month-cell-foot { display: none; }
  .month-dow { font-size: 10px; }
  .ml-hint { display: none; }
}

/* Month hover tooltip: who's on shift for a section pill / cover dot */
.month-tip {
  position: fixed;
  z-index: 1000;
  max-width: 240px;
  padding: 8px 10px;
  background: #1f2933;
  color: #fff;
  border-radius: 8px;
  font-size: 12px;
  line-height: 1.4;
  box-shadow: 0 8px 24px rgba(15, 23, 42, 0.28);
  pointer-events: none; /* never let the tip swallow hover/click */
}
.month-tip.hidden { display: none; }
.month-tip .tip-head {
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  opacity: 0.75;
  margin-bottom: 5px;
}
.month-tip .tip-row { display: flex; flex-wrap: wrap; align-items: center; gap: 3px 7px; padding: 1px 0; }
.month-tip .tip-dot { width: 8px; height: 8px; border-radius: 50%; flex: none; }
.month-tip .tip-name { font-weight: 600; }
.month-tip .tip-time {
  margin-left: auto;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  opacity: 0.85;
}
.month-tip .tip-co { flex-basis: 100%; margin-left: 15px; opacity: 0.7; }

/* The section pills invite a hover on desktop */
.month-badge { cursor: help; }
