:root {
  --cell-w: 40px;
  --row-h: 34px;
  --bg: #eef0f3;
  --panel: #ffffff;
  --line: #d5d9e0;
  --line-strong: #8a93a3;
  --ink: #1f2933;
  --muted: #64707d;
  --accent: #2563eb;
  --ok: #bff0c4;
  --no: #f6d0d0;
  --bad: #d62828;
  --detail-bg: #4a5568;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  font-size: 14px;
  color: var(--ink);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); }
button, input, select { font: inherit; color: inherit; }

#app { display: flex; flex-direction: column; height: 100vh; }

/* ---------- top bar ---------- */
.topbar {
  display: flex; flex-wrap: wrap; align-items: center; gap: 6px 14px;
  padding: 6px 12px; background: var(--panel); border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; font-size: 17px; color: var(--accent); margin-right: 4px; }
.group { display: flex; align-items: center; gap: 4px; padding-right: 14px; border-right: 1px solid var(--line); }
.group:last-child { border-right: 0; }
.topbar button, .modal button, .detail button {
  background: #f6f7f9; border: 1px solid var(--line); border-radius: 6px; padding: 4px 10px; cursor: pointer;
}
.topbar button:hover:not(:disabled) { background: #e8ecf3; }
.topbar button:disabled { opacity: .4; cursor: default; }
.topbar select { padding: 4px 6px; border: 1px solid var(--line); border-radius: 6px; max-width: 220px; }
.tool { display: flex; flex-direction: column; align-items: center; font-size: 12px; padding: 3px 8px !important; line-height: 1.2; }
.tool span { font-size: 18px; }
.seg { gap: 0; }
.seg button { border-radius: 0; margin-left: -1px; }
.seg button:first-child { border-radius: 6px 0 0 6px; }
.seg button:last-child { border-radius: 0 6px 6px 0; }
.seg button.on { background: var(--accent); color: #fff; border-color: var(--accent); }

/* ---------- grid ---------- */
.grid-wrap { flex: 1; overflow: auto; background: var(--panel); }
.empty { padding: 40px; color: var(--muted); text-align: center; }
table.grid { border-collapse: separate; border-spacing: 0; font-size: 12px; }
.grid th { background: #f3f4f7; font-weight: 600; color: var(--muted); }
.grid thead th { position: sticky; z-index: 2; }
.grid thead tr:first-child th { top: 0; height: 22px; }
.grid thead tr:nth-child(2) th { top: 22px; height: 18px; font-size: 10px; border-bottom: 1px solid var(--line-strong); }
.grid .corner { left: 0; z-index: 3 !important; min-width: 64px; border-right: 1px solid var(--line-strong); border-bottom: 1px solid var(--line-strong); }
.grid .day { color: var(--ink); }
.grid .per { min-width: var(--cell-w); width: var(--cell-w); }
.grid .rowh {
  position: sticky; left: 0; z-index: 1; padding: 0 8px; text-align: left; white-space: nowrap; cursor: pointer;
  border-right: 1px solid var(--line-strong); border-bottom: 1px solid var(--line); color: var(--ink);
}
.grid .rowh:hover { background: #e3e8f2; }
.grid tr.rowsel .rowh { background: #fde68a; }
.grid .brk { min-width: 8px !important; width: 8px !important; padding: 0; background: repeating-linear-gradient(45deg, #e4e6ea 0 3px, #f1f2f4 3px 6px); }
.grid .dend { border-right: 2px solid var(--line-strong) !important; }
td.cell {
  width: var(--cell-w); min-width: var(--cell-w); max-width: var(--cell-w); height: var(--row-h);
  padding: 1px; border-right: 1px solid var(--line); border-bottom: 1px solid var(--line); vertical-align: top;
}
tr.rowsel td.cell { background: #fffbeb; }
td.cell.na { background: repeating-linear-gradient(-45deg, #fde2e2 0 4px, #fff 4px 8px); }
td.cell.ok { background: var(--ok); cursor: pointer; }
td.cell.no { background: var(--no); }
td.cell.brk.ok, td.cell.brk.no { background: repeating-linear-gradient(45deg, #e4e6ea 0 3px, #f1f2f4 3px 6px); }

.stack { display: flex; flex-direction: column; gap: 1px; height: calc(var(--row-h) - 2px); }
.card {
  flex: 1; min-height: 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
  border-radius: 3px; overflow: hidden; cursor: grab; line-height: 1.05; user-select: none;
  box-shadow: inset 0 0 0 1px rgba(0,0,0,.15);
}
.card b { font-size: 11px; }
.card i { font-style: normal; font-size: 9px; opacity: .85; white-space: nowrap; max-width: 100%; overflow: hidden; text-overflow: ellipsis; }
.stack.multi .card i { display: none; }
.card.cont { background-image: linear-gradient(rgba(255,255,255,.18), rgba(255,255,255,.18)); }
.card.sel { outline: 2px solid #000; outline-offset: -2px; box-shadow: 0 0 0 2px #fff inset; }
.card.bad { box-shadow: inset 0 0 0 2px var(--bad); position: relative; }
.card.bad::after { content: ""; position: absolute; top: 0; right: 0; border: 5px solid transparent; border-top-color: var(--bad); border-right-color: var(--bad); }

/* ---------- bottom: detail + tray ---------- */
.bottom { display: grid; grid-template-columns: 300px 1fr; height: 170px; border-top: 1px solid var(--line-strong); }
.detail { background: var(--detail-bg); color: #f1f5f9; padding: 10px 14px; overflow: auto; }
.detail h2 { margin: 0; font-size: 17px; color: #fde047; }
.dhead { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; }
.swatch { width: 18px; height: 18px; border-radius: 4px; border: 1px solid rgba(255,255,255,.5); flex: none; }
.detail dl { display: grid; grid-template-columns: auto 1fr; gap: 2px 10px; margin: 0; font-size: 13px; }
.detail dt { color: #cbd5e1; }
.detail dd { margin: 0; }
.detail select { max-width: 100%; padding: 1px 4px; border-radius: 4px; border: 0; color: var(--ink); }
.detail .hint { color: #cbd5e1; }
.detail .warn { margin: 6px 0; padding-left: 0; list-style: none; color: #fecaca; font-size: 12px; }
.dacts { margin-top: 6px; }
.detail button { color: var(--ink); }

.tray { background: #f8f9fb; display: flex; flex-direction: column; min-width: 0; }
.tray-head { display: flex; gap: 12px; align-items: center; padding: 6px 10px; font-size: 13px; border-bottom: 1px solid var(--line); }
.tray-items { flex: 1; overflow: auto; padding: 8px 10px; display: flex; flex-wrap: wrap; align-content: flex-start; gap: 6px; }
.tcard {
  position: relative; width: calc(var(--cell-w) * var(--dur) + 18px); min-width: 46px; height: 40px;
  border-radius: 4px; display: flex; flex-direction: column; align-items: center; justify-content: center;
  cursor: grab; user-select: none; line-height: 1.1; box-shadow: 2px 2px 0 rgba(0,0,0,.18), inset 0 0 0 1px rgba(0,0,0,.15);
}
.tcard b { font-size: 12px; }
.tcard i { font-style: normal; font-size: 9px; white-space: nowrap; max-width: 100%; overflow: hidden; text-overflow: ellipsis; padding: 0 2px; }
.tcard .n { position: absolute; top: -6px; right: -6px; background: #111827; color: #fff; font-size: 10px; border-radius: 8px; padding: 0 5px; }
.tcard.sel { outline: 2px solid #000; outline-offset: 1px; }

.hint { color: var(--muted); font-size: 12px; }
.chip { display: inline-flex; align-items: center; gap: 2px; background: #e0e7ff; color: #1e3a8a; border-radius: 10px; padding: 0 4px 0 8px; font-size: 12px; }
.chip button { border: 0; background: transparent; cursor: pointer; padding: 0 3px; font-size: 13px; line-height: 1; }

/* ---------- status bar ---------- */
.status { display: flex; gap: 18px; align-items: center; padding: 3px 12px; background: #e5e7eb; font-size: 12px; border-top: 1px solid var(--line); }
.status .msg { flex: 1; color: var(--bad); min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.status .bad { color: var(--bad); font-weight: 600; }
.status .good { color: #15803d; }
.zoom { display: flex; align-items: center; gap: 6px; }
.zoom input { width: 110px; }

/* ---------- modal ---------- */
.overlay { position: fixed; inset: 0; background: rgba(15,23,42,.45); display: flex; align-items: center; justify-content: center; z-index: 50; padding: 16px; }
.modal { background: var(--panel); border-radius: 10px; width: min(1180px, 100%); max-height: 92vh; display: flex; flex-direction: column; box-shadow: 0 20px 50px rgba(0,0,0,.3); }
.modal.small { width: min(460px, 100%); }
.modal header { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 12px; border-bottom: 1px solid var(--line); }
.modal header h3 { margin: 0; }
.modal .x { font-size: 20px; line-height: 1; padding: 2px 10px; }
.tabs { display: flex; flex-wrap: wrap; gap: 4px; }
.tabs button.on { background: var(--accent); color: #fff; border-color: var(--accent); }
.modal-body { overflow: auto; padding: 12px 16px 20px; }
.modal-body h3 { margin: 14px 0 6px; font-size: 15px; }
.modal-body section:first-child h3 { margin-top: 0; }
.form { display: flex; flex-direction: column; gap: 10px; }
.form.inline { flex-direction: row; flex-wrap: wrap; }
.form label { display: flex; flex-direction: column; gap: 3px; font-size: 12px; color: var(--muted); }
.form input, .form select { padding: 5px 8px; border: 1px solid var(--line); border-radius: 6px; color: var(--ink); min-width: 200px; }
.actions { display: flex; justify-content: flex-end; }
button.primary { background: var(--accent) !important; color: #fff; border-color: var(--accent) !important; }
.toolbar-row { display: flex; gap: 16px; align-items: center; margin-bottom: 6px; }
.toolbar-row select { padding: 3px 6px; border: 1px solid var(--line); border-radius: 6px; }

table.etable { border-collapse: collapse; margin: 4px 0 8px; }
.etable th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 600; padding: 4px 6px; border-bottom: 1px solid var(--line); }
.etable td { padding: 3px 6px; border-bottom: 1px solid #eef0f3; vertical-align: middle; }
.etable input:not([type=checkbox]):not([type=color]), .etable select { padding: 3px 6px; border: 1px solid var(--line); border-radius: 5px; }
.etable input[type=text] { width: 200px; }
.etable input[type=color] { width: 40px; height: 26px; padding: 0; border: 1px solid var(--line); border-radius: 5px; }
.rowacts { white-space: nowrap; }
.rowacts button { padding: 2px 7px; }
button.danger:hover { background: #fee2e2; border-color: #fca5a5; }
button.add { margin-top: 2px; }
.chips { display: flex; flex-wrap: wrap; gap: 3px; align-items: center; max-width: 230px; }
.chips select { padding: 1px 2px !important; width: 42px; }
.pending { color: #b45309; font-weight: 600; }
.done { color: #15803d; }

table.avail { border-collapse: collapse; margin-top: 8px; }
.avail th, .avail td { padding: 3px 6px; border: 1px solid var(--line); text-align: center; }
.avail th small { color: var(--muted); font-weight: 400; }
.avail td.brk { background: repeating-linear-gradient(45deg, #e4e6ea 0 3px, #f1f2f4 3px 6px); }
.avail .slot { width: 56px; background: #dcfce7; border-color: #86efac; }
.avail .slot.off { background: #fee2e2; border-color: #fca5a5; }

/* ---------- print ---------- */
@media print {
  #app { height: auto; }
  .topbar, .bottom, .status, #modal { display: none !important; }
  .grid-wrap { overflow: visible; }
  td.cell.ok, td.cell.no { background: none; }
  .card { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}

/* ---------- rules ---------- */
:root { --soft: #d97706; --warn-cell: #fde9b8; }
.topbar button[data-act="lock"].on { background: #fde68a; border-color: #f59e0b; }
.status .soft { color: var(--soft); font-weight: 600; }
.card.warn { box-shadow: inset 0 0 0 2px var(--soft); position: relative; }
.card.warn::after { content: ""; position: absolute; top: 0; right: 0; border: 4px solid transparent; border-top-color: var(--soft); border-right-color: var(--soft); }
td.cell.ok.warn { background: linear-gradient(135deg, var(--ok) 50%, var(--warn-cell) 50%); }
td.cell.blk { background: repeating-linear-gradient(-45deg, #e0e7ff 0 4px, #fff 4px 8px); }
.blk-label { display: block; font-size: 9px; color: #3730a3; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; text-align: center; line-height: calc(var(--row-h) - 2px); }
.grid-wrap.locking td.cell:not(.brk) { cursor: cell; }
.grid-wrap.locking td.cell:not(.brk):hover { outline: 2px solid #f59e0b; outline-offset: -2px; }
.detail .warn li.soft { color: #fde68a; }

.radd { display: flex; flex-wrap: wrap; gap: 6px; margin: 8px 0 12px; }
.rules { display: flex; flex-direction: column; gap: 8px; margin-bottom: 10px; }
.rule { border: 1px solid var(--line); border-left: 4px solid var(--accent); border-radius: 8px; padding: 6px 10px; background: #fafbfc; }
.rule.off { opacity: .55; border-left-color: var(--line-strong); }
.rhead { display: flex; align-items: center; gap: 8px; }
.ricon { font-size: 16px; width: 20px; text-align: center; }
.rdesc { flex: 1; color: var(--muted); font-size: 12px; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rhead select { padding: 2px 6px; border-radius: 5px; border: 1px solid var(--line); }
.rhead select.hard { background: #fee2e2; border-color: #fca5a5; }
.rhead select.soft { background: #fef3c7; border-color: #fcd34d; }
.rbody { display: flex; flex-wrap: wrap; gap: 6px 18px; margin: 6px 0 2px 28px; }
.rf { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.rf input, .rf select { padding: 3px 6px; border: 1px solid var(--line); border-radius: 5px; }
.rf small { color: var(--muted); font-style: italic; }
.avail .slot.yes { background: #bbf7d0; border-color: #22c55e; font-weight: 600; }
.avail.rule .slot:not(.off):not(.yes) { background: #f8fafc; border-color: var(--line); }
.avail .hdr { background: transparent !important; border: 0 !important; padding: 0 2px !important; font-weight: 600; text-decoration: underline dotted; }

/* ---------- login, user, save status ---------- */
.spacer { flex: 1; }
.topbar .group.user { border-right: 0; padding-right: 0; }
.local-tag { font-size: 12px; color: var(--muted); white-space: nowrap; }
.login { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px; }
.login-box { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 28px 30px; width: min(380px, 100%);
  display: flex; flex-direction: column; gap: 12px; box-shadow: 0 10px 30px rgba(15,23,42,.08); }
.login-box label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
.login-box input { padding: 8px 10px; border: 1px solid var(--line); border-radius: 6px; font-size: 15px; color: var(--ink); }
.login-box button { padding: 9px; border-radius: 6px; border: 1px solid var(--accent); font-size: 15px; cursor: pointer; }
.brand.big { font-size: 26px; }
.err { color: var(--bad); font-size: 13px; margin: 0; }
.okmsg { color: #15803d; font-size: 13px; margin: 0 0 8px; }
.empty button { margin-top: 8px; padding: 8px 14px; border-radius: 6px; border: 1px solid var(--accent); cursor: pointer; }
.status .save { white-space: nowrap; }
.status .save.ok { color: #15803d; }
.status .save.bad { color: var(--bad); font-weight: 600; }
.status .save button { font-size: 11px; padding: 0 6px; margin-left: 4px; border: 1px solid var(--line); border-radius: 4px; background: #fff; cursor: pointer; }

/* ---------- admin ---------- */
.notice { background: #fef9c3; border: 1px solid #fde047; border-radius: 8px; padding: 8px 12px; margin: 6px 0 10px; font-size: 13px; }
.notice button { margin-left: 6px; }
table.etable.wide { width: 100%; }
.chip.plain { padding: 1px 8px; }
.chip.admin { background: #fde68a; color: #78350f; padding: 1px 8px; }
tr.inactive td { color: var(--muted); }
.userform { border: 1px solid var(--line); border-radius: 8px; padding: 10px 14px; margin-bottom: 12px; background: #fafbfc; }
.userform h3 { margin-top: 0 !important; }
.centre-picks { display: flex; flex-wrap: wrap; gap: 4px 14px; margin: 10px 0; font-size: 13px; }
.centre-picks .lbl, .exp .lbl { font-size: 12px; color: var(--muted); width: 100%; }
.inline-check { font-size: 13px; display: flex; align-items: center; gap: 6px; }
.userform .actions, .exp .actions { gap: 6px; margin-top: 8px; }

/* ---------- export ---------- */
.modal.mid { width: min(720px, 100%); }
.exp-row { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 16px; margin-bottom: 12px; }
.exp-row.top { align-items: flex-start; }
.exp .exp-row .lbl { width: 80px; font-weight: 600; color: var(--ink); }
.exp .opt { font-size: 13px; display: flex; align-items: center; gap: 5px; }
.exp-pick { flex: 1; min-width: 260px; border: 1px solid var(--line); border-radius: 8px; }
.exp-pick-head { display: flex; gap: 6px; align-items: center; padding: 4px 8px; border-bottom: 1px solid var(--line); font-size: 12px; color: var(--muted); }
.exp-pick-head span { flex: 1; }
.exp-pick-head button { padding: 1px 8px !important; font-size: 12px; }
.exp-items { max-height: 220px; overflow: auto; padding: 6px 8px; display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 2px 10px; font-size: 13px; }
.exp-items label { display: flex; gap: 5px; align-items: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.exp .seg button.on { background: var(--accent); color: #fff; border-color: var(--accent); }
.exp .seg button { border-radius: 0; margin-left: -1px; }
.exp .seg button:first-child { border-radius: 6px 0 0 6px; }
.exp .seg button:last-child { border-radius: 0 6px 6px 0; }

/* ---------- guards and absences ---------- */
.grid tfoot th, .grid tfoot td { position: sticky; bottom: 0; z-index: 2; border-top: 2px solid var(--line-strong); }
.grid tfoot th.rowh { z-index: 3; background: #eef2ff; }
.grid td.cov { background: #eef2ff; text-align: center; font-weight: 600; font-size: 11px; color: #3730a3; border-right: 1px solid var(--line); }
.grid td.cov.short { background: #fde2e2; color: var(--bad); }
.abs-grid { display: grid; grid-template-columns: minmax(280px, 1fr) 2fr; gap: 20px; }
@media (max-width: 900px) { .abs-grid { grid-template-columns: 1fr; } }
.abs-form { display: flex; flex-wrap: wrap; gap: 6px; align-items: flex-end; margin-bottom: 8px; }
.abs-form label { display: flex; flex-direction: column; font-size: 11px; color: var(--muted); }
.abs-form input, .abs-form select { padding: 4px 6px; border: 1px solid var(--line); border-radius: 5px; }
.abs-form input[name=reason] { flex: 1; min-width: 140px; }
.abs-list { width: 100%; font-size: 13px; }
.abs-list tr.now td { background: #fef9c3; }
.abs-list tr.past td { color: var(--muted); }
.abs-day { display: flex; align-items: center; gap: 6px; margin-bottom: 8px; flex-wrap: wrap; }
.abs-day input { padding: 3px 6px; border: 1px solid var(--line); border-radius: 5px; }
.abs-dayname { margin-left: 8px; text-transform: capitalize; }
.abs-subs tr.todo td { background: #fff1f2; }
.abs-subs tr.ok td { background: #f0fdf4; }
.abs-subs select { max-width: 320px; }
.abs-subs .swatch { display: inline-block; width: 10px; height: 10px; vertical-align: middle; border: 0; }
