:root {
  --bg: #f3f4f6;
  --surface: #ffffff;
  --surface-2: #f9fafb;
  --border: #d9dde3;
  --text: #111827;
  --muted: #6b7280;
  --accent: #1d4ed8;
  --accent-text: #ffffff;
  --danger: #b91c1c;
  --today: #eff6ff;
  --drop: #dbeafe;
  --radius: 8px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1218;
    --surface: #181c24;
    --surface-2: #1f2430;
    --border: #2e3442;
    --text: #e8eaee;
    --muted: #9aa3b2;
    --accent: #6b9bff;
    --accent-text: #0b1020;
    --danger: #f87171;
    --today: #17233b;
    --drop: #1f3357;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.4 "Segoe UI", system-ui, -apple-system, sans-serif;
  display: flex;
  flex-direction: column;
}
h1, h2, h3, p { margin: 0; }
button, input, select, textarea { font: inherit; color: inherit; }

button {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 12px;
  cursor: pointer;
}
button:hover { background: var(--surface-2); }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
button.primary:hover { filter: brightness(1.08); }
button.danger { color: var(--danger); border-color: transparent; background: transparent; }
button.danger:hover { background: color-mix(in srgb, var(--danger) 12%, transparent); }
button.small { padding: 4px 10px; font-size: 13px; }
button.icon { padding: 2px 10px; font-size: 20px; line-height: 1.2; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

input, select, textarea {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 7px 9px;
}
textarea { resize: vertical; }

/* ---------- top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px 20px;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar h1 { font-size: 18px; }
.nav { display: flex; align-items: center; gap: 6px; flex: 1; min-width: 260px; }
.nav h2 { font-size: 17px; font-weight: 600; margin-left: 8px; }
.views { display: flex; }
.views button { border-radius: 0; margin-left: -1px; }
.views button:first-child { border-radius: 6px 0 0 6px; margin-left: 0; }
.views button:last-child { border-radius: 0 6px 6px 0; }
.views button[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; position: relative; }
.mode { font-size: 12px; color: var(--muted); border: 1px dashed var(--border); border-radius: 999px; padding: 3px 10px; }

.mode.error { color: var(--danger); border-color: var(--danger); border-style: solid; }
.mode.busy { border-style: solid; }

.signin { position: fixed; inset: 0; z-index: 10; background: var(--bg); display: grid; place-items: center; padding: 16px; }
.signin[hidden] { display: none; }
.signin-box { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 32px; max-width: 380px; text-align: center; display: flex; flex-direction: column; gap: 14px; }

/* people without edit permission, and jobs that come from a SharePoint list */
.readonly .edit-only, .readonly #newJob, .readonly .day-add, .readonly .worker .x, .readonly dialog .danger, .readonly dialog .primary { display: none; }
.readonly .job, .readonly .entry, .readonly .worker { cursor: default; }
/* workers that come from the company directory can't be added or removed here */
.dirworkers #workerForm, .dirworkers #workerList .x { display: none; }
.dirworkers #workerList .worker { padding-right: 10px; }

/* copying a scheduled job to other days */
.pastebar { display: flex; align-items: center; justify-content: center; gap: 12px; padding: 7px 16px; background: var(--accent); color: var(--accent-text); font-weight: 600; }
.pastebar[hidden] { display: none; }
.pasting .day { cursor: copy; }
.pasting .day:hover { background: var(--drop); border-color: var(--accent); border-style: dashed; }
.entry-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 4px; }
.entry .copy { flex: none; border: 0; background: transparent; padding: 0 4px; font-size: 14px; line-height: 1.2; color: var(--muted); border-radius: 4px; }
.entry .copy:hover { color: var(--text); background: color-mix(in srgb, var(--c) 30%, var(--surface)); }
.view-month .entry .copy, .readonly .entry .copy, .readonly #entryCopy { display: none; }

/* ---------- layout ---------- */
.layout { flex: 1; min-height: 0; display: grid; grid-template-columns: 460px 1fr; }
/* Jobs and Workers sit side by side, each scrolling on its own */
.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 3fr) minmax(0, 2fr);
}
.sidebar section { min-height: 0; min-width: 0; overflow-y: auto; padding: 14px; }
.sidebar section + section { border-left: 1px solid var(--border); }
.side-head { display: flex; align-items: center; justify-content: space-between; min-height: 28px; }
.side-head h3 { font-size: 15px; }
.hint { color: var(--muted); font-size: 12px; margin: 2px 0 8px; }
.list { display: flex; flex-direction: column; gap: 6px; }
.list.chips { flex-direction: row; flex-wrap: wrap; }
.group-head { flex: 0 0 100%; font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; color: var(--text); margin-top: 6px; padding-bottom: 2px; border-bottom: 1px solid var(--border); }
.group-head:first-child { margin-top: 0; }
.empty { color: var(--muted); font-size: 13px; padding: 10px; border: 1px dashed var(--border); border-radius: var(--radius); text-align: center; width: 100%; }
#jobSearch { margin-bottom: 8px; }
.add-row { display: flex; gap: 6px; margin-bottom: 8px; }

.job {
  border: 1px solid var(--border);
  border-left: 5px solid var(--c);
  border-radius: var(--radius);
  background: color-mix(in srgb, var(--c) 9%, var(--surface));
  padding: 7px 9px;
  cursor: grab;
}
.job:hover { border-color: var(--c); }
.job .name { font-weight: 600; overflow-wrap: anywhere; }
.job .meta { font-size: 12px; color: var(--muted); overflow-wrap: anywhere; }

.worker {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 3px 4px 3px 10px;
  cursor: grab;
  max-width: 100%;
}
.worker .wname { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.worker .x {
  border: 0; background: transparent; padding: 0 5px; border-radius: 999px;
  color: var(--muted); font-size: 14px; line-height: 1.3;
}
.worker .x:hover { color: var(--danger); background: color-mix(in srgb, var(--danger) 14%, transparent); }
.worker.busy { opacity: .55; }
.worker.conflict { border-color: var(--danger); color: var(--danger); }
.dragging { opacity: .4; }

/* ---------- calendar ---------- */
.calendar { min-width: 0; min-height: 0; display: flex; flex-direction: column; padding: 12px; gap: 6px; }
.weekdays { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 6px; }
.weekdays div { text-align: center; font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.grid { flex: 1; min-height: 0; display: grid; gap: 6px; grid-template-columns: repeat(7, minmax(0, 1fr)); grid-auto-rows: minmax(0, 1fr); }
.view-day .grid { grid-template-columns: minmax(0, 1fr); }

.day {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
}
.day.today { background: var(--today); border-color: var(--accent); }
.day.outside { opacity: .55; }
.day.selected { box-shadow: 0 0 0 2px var(--accent); border-color: var(--accent); }
.day.drop { background: var(--drop); border-color: var(--accent); border-style: dashed; }
.day-head { display: flex; align-items: center; justify-content: space-between; padding: 4px 4px 2px 8px; }
.day-label { border: 0; background: transparent; padding: 2px 4px; font-weight: 600; text-align: left; }
.day-label:hover { text-decoration: underline; background: transparent; }
.day.today .day-label { color: var(--accent); }
.day-add { border: 0; background: transparent; color: var(--muted); padding: 0 7px; font-size: 18px; line-height: 1.2; }
.day-body { flex: 1; min-height: 0; overflow-y: auto; padding: 4px 6px 8px; display: flex; flex-direction: column; gap: 6px; }
.view-day .day-body { padding: 8px 14px 14px; gap: 10px; }
.view-day .day-head { padding: 8px 10px 2px 14px; font-size: 16px; }
.day-empty { color: var(--muted); font-size: 12px; text-align: center; margin: auto 0; padding: 12px 4px; }

.entry {
  border: 1px solid color-mix(in srgb, var(--c) 45%, var(--border));
  border-left: 5px solid var(--c);
  border-radius: 6px;
  background: color-mix(in srgb, var(--c) 13%, var(--surface));
  padding: 6px 8px;
  cursor: grab;
  display: flex;
  flex-direction: column;
  gap: 3px;
}
.entry.drop { outline: 2px dashed var(--c); outline-offset: 1px; background: color-mix(in srgb, var(--c) 28%, var(--surface)); }
.entry .title { font-weight: 600; overflow-wrap: anywhere; }
.entry .time { font-size: 12px; font-weight: 600; }
.entry .addr, .entry .notes { font-size: 12px; color: var(--muted); overflow-wrap: anywhere; white-space: pre-wrap; }
.entry .crew { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 2px; }
.entry .crew .worker { background: var(--surface); font-size: 12px; padding: 1px 2px 1px 8px; }
.entry .crew .wname { white-space: normal; overflow-wrap: anywhere; }
.entry .no-crew { font-size: 12px; color: var(--muted); font-style: italic; }
.view-day .entry { padding: 10px 14px; font-size: 15px; }
.view-day .entry .time, .view-day .entry .addr, .view-day .entry .notes, .view-day .entry .crew .worker { font-size: 13px; }

/* month view: compact cards */
.view-month .day-head { padding: 2px 2px 0 4px; }
.view-month .day-label { font-size: 12px; }
.view-month .day-add { font-size: 15px; }
.view-month .day-body { padding: 2px 4px 4px; gap: 3px; }
.view-month .entry { padding: 2px 6px; gap: 0; border-left-width: 4px; }
.view-month .entry .title { font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.view-month .entry .time { font-size: 11px; font-weight: 400; }
.view-month .entry .addr, .view-month .entry .notes, .view-month .entry .no-crew, .view-month .day-empty { display: none; }
.view-month .entry .crew .worker { font-size: 11px; padding: 0 6px; }
.view-month .entry .crew .x { display: none; }

/* ---------- dialogs ---------- */
dialog {
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  padding: 20px;
  width: min(440px, calc(100vw - 32px));
  box-shadow: 0 20px 50px rgba(0, 0, 0, .3);
}
dialog::backdrop { background: rgba(0, 0, 0, .45); }
dialog form { display: flex; flex-direction: column; gap: 12px; }
dialog h3 { font-size: 17px; }
dialog label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; font-weight: 600; }
dialog label input, dialog label select, dialog label textarea { font-weight: 400; }
dialog fieldset { border: 0; padding: 0; margin: 0; }
dialog legend { font-size: 13px; font-weight: 600; padding: 0; margin-bottom: 6px; }
dialog .hint:empty { display: none; }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.actions { display: flex; gap: 8px; align-items: center; margin-top: 4px; }
.spacer { flex: 1; }
.swatches { display: flex; flex-wrap: wrap; gap: 8px; }
.swatches label { display: block; }
.swatches input { position: absolute; opacity: 0; width: 0; height: 0; }
.swatches span { display: block; width: 28px; height: 28px; border-radius: 50%; background: var(--c); cursor: pointer; border: 2px solid var(--surface); box-shadow: 0 0 0 1px var(--border); }
.swatches input:checked + span { box-shadow: 0 0 0 3px var(--text); }
.swatches input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 3px; }
.checks { display: flex; flex-wrap: wrap; gap: 6px 14px; max-height: 150px; overflow-y: auto; }
.checks label { flex-direction: row; align-items: center; gap: 6px; font-weight: 400; }
.checks input { width: auto; }

/* ---------- small screens ---------- */
@media (max-width: 820px) {
  body { height: auto; }
  .layout { grid-template-columns: 1fr; }
  .sidebar { border-right: 0; border-bottom: 1px solid var(--border); }
  .sidebar section { max-height: 45vh; }
  .calendar { min-height: 80vh; }
  .view-week .grid { grid-template-columns: minmax(0, 1fr); grid-auto-rows: auto; }
  .view-week .day { min-height: 90px; }
}
