:root {
  --bg: #f6f7fb;
  --panel: #ffffff;
  --ink: #1f2430;
  --muted: #6b7280;
  --line: #e4e7ee;
  --primary: #6d4aff;
  --primary-ink: #ffffff;
  --bad: #c02a37;
  --warn: #9a6700;
  --ok: #1a7f4b;
  --lab-tint: #eef4ff;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

/* Top bar */
.topbar {
  display: flex; align-items: center; gap: 24px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  padding: 0 20px; height: 54px;
  position: sticky; top: 0; z-index: 20;
}
.brand { font-weight: 700; letter-spacing: .2px; }
.brand span { color: var(--primary); }
.mainnav { display: flex; gap: 4px; align-items: center; margin-left: auto; }
.mainnav a {
  color: var(--muted); text-decoration: none; padding: 7px 12px; border-radius: 8px;
  font-weight: 500;
}
.mainnav a:hover { background: #f0f1f6; color: var(--ink); }
.mainnav a.active { background: #efeaff; color: var(--primary); }
.mainnav .export-link { margin-left: 6px; color: var(--primary); }
.mainnav .add-school-link { color: var(--primary); }
.schoolpick { margin-right: 4px; }
.schoolpick select {
  font: inherit; font-weight: 600; font-size: 13px; padding: 6px 10px;
  border: 1px solid var(--line); border-radius: 8px; background: #f7f7fb; color: var(--ink); cursor: pointer;
}
.schoolpick select:hover { background: #efeaff; }

.formcard {
  max-width: 540px; margin: 8px auto; background: var(--panel);
  border: 1px solid var(--line); border-radius: 14px; padding: 22px 24px;
}
.schoolform { display: flex; flex-direction: column; gap: 14px; margin-top: 8px; }
.schoolform input[type=text] { font: inherit; font-size: 15px; padding: 9px 11px; border: 1px solid var(--line); border-radius: 9px; }
.schoolform input[type=file] { font: inherit; font-size: 13px; }
.schoolform input:focus { outline: 2px solid var(--primary); outline-offset: -1px; border-color: var(--primary); }
.formfoot { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }

/* Auth: user menu, roles, login, flash */
.usermenu { display: flex; align-items: center; gap: 12px; margin-left: 14px; padding-left: 14px; border-left: 1px solid var(--line); }
.usermenu .users-link { color: var(--muted); text-decoration: none; padding: 7px 10px; border-radius: 8px; font-weight: 500; }
.usermenu .users-link:hover { background: #f0f1f6; color: var(--ink); }
.usermenu .users-link.active { background: #efeaff; color: var(--primary); }
.who { font-size: 13px; font-weight: 600; display: inline-flex; align-items: center; gap: 6px; }
.role { font-weight: 600; font-size: 10px; text-transform: uppercase; letter-spacing: .4px; padding: 2px 6px; border-radius: 999px; }
.role-admin { background: #efeaff; color: #6d4aff; }
.role-scheduler { background: #e6f6ee; color: #15663f; }
.role-viewer { background: #eef0f5; color: #5b6472; }
.logoutform { margin: 0; }
.logout-btn { font: inherit; font-size: 13px; border: 1px solid var(--line); background: #fff; color: var(--ink); padding: 6px 12px; border-radius: 8px; cursor: pointer; }
.logout-btn:hover { background: #f4f5f9; }

.loginbody { background: var(--bg); display: flex; min-height: 100vh; align-items: center; justify-content: center; }
.loginwrap { width: 340px; max-width: 92vw; }
.loginbrand { font-size: 22px; font-weight: 700; text-align: center; margin-bottom: 16px; }
.loginbrand span { color: var(--primary); }
.loginform { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 22px; display: flex; flex-direction: column; gap: 13px; box-shadow: 0 10px 40px rgba(0,0,0,.06); }
.loginform input { font: inherit; font-size: 15px; padding: 9px 11px; border: 1px solid var(--line); border-radius: 9px; }
.loginform input:focus { outline: 2px solid var(--primary); outline-offset: -1px; border-color: var(--primary); }

.flash { margin-bottom: 14px; display: flex; flex-direction: column; gap: 8px; }
.flash-item { padding: 10px 13px; border-radius: 9px; font-size: 13.5px; }
.flash-success { background: #e6f6ee; color: #15663f; }
.flash-error { background: #fdeef0; color: var(--bad); }

.grow { flex: 1; }
.chip { display: inline-block; background: #eef0f5; color: #444; border-radius: 999px; padding: 2px 9px; font-size: 12px; margin: 0 4px 2px 0; }
.ok-dot { color: #15663f; font-weight: 600; font-size: 12.5px; }
.checklist { display: flex; flex-wrap: wrap; gap: 6px 16px; margin-top: 6px; }
.chk2 { display: inline-flex; align-items: center; gap: 6px; font-size: 13.5px; color: var(--ink); }
.toolbar h1.page-h1 { margin: 0; }

.content { padding: 18px 20px 60px; }

/* Toolbar / filters */
.toolbar {
  display: flex; flex-wrap: wrap; gap: 14px 24px; align-items: center;
  margin-bottom: 14px;
}
.daytabs { display: flex; gap: 4px; background: #eceef4; padding: 4px; border-radius: 10px; }
.daytab {
  text-decoration: none; color: var(--muted); padding: 6px 13px; border-radius: 7px;
  font-weight: 600; font-size: 13px;
}
.daytab:hover { color: var(--ink); }
.daytab.active { background: var(--panel); color: var(--primary); box-shadow: 0 1px 2px rgba(0,0,0,.08); }
.daytab.weekend { font-style: italic; }
.filters { display: flex; gap: 16px; align-items: center; flex-wrap: wrap; }
.filters label { color: var(--muted); font-size: 13px; display: inline-flex; gap: 6px; align-items: center; }
.filters select { font: inherit; padding: 5px 8px; border: 1px solid var(--line); border-radius: 7px; background: #fff; }
.filters .chk { user-select: none; }
.count { color: var(--muted); font-size: 13px; }
.count a { color: var(--primary); }

/* Grid table */
.gridwrap { overflow: auto; border: 1px solid var(--line); border-radius: 12px; background: var(--panel); }
table.grid { border-collapse: collapse; width: 100%; min-width: 980px; }
table.grid th, table.grid td { border: 1px solid var(--line); }
table.grid thead th {
  position: sticky; top: 0; background: #fafbfe; z-index: 5;
  font-size: 12px; padding: 6px 8px; text-align: center; color: var(--muted);
}
.band th { font-weight: 700; color: var(--ink); }
.band .band-lab { background: var(--lab-tint); }
.band .corner { background: var(--primary); color: #fff; font-size: 14px; }
.roomrow .room { font-weight: 600; color: var(--ink); white-space: nowrap; }
.roomrow .room.lab { background: var(--lab-tint); }
.periodhead {
  background: #fafbfe; font-size: 12px; color: var(--muted); white-space: nowrap;
  padding: 6px 10px; text-align: right; min-width: 92px;
}
.periodhead small { display: block; font-weight: 400; opacity: .8; }

td.cell { height: 56px; min-width: 86px; padding: 3px; vertical-align: top; position: relative; }
.editable td.cell.empty { cursor: pointer; }
.editable td.cell.empty:hover { background: #f3f0ff; }
.editable td.cell.empty:hover::after {
  content: "+"; position: absolute; inset: 0; display: flex; align-items: center;
  justify-content: center; font-size: 22px; font-weight: 300; color: var(--primary); opacity: .6;
}
td.cell.drop-ok { outline: 2px dashed var(--ok); outline-offset: -3px; background: #eafaf0; }
td.cell.drop-bad { outline: 2px dashed var(--bad); outline-offset: -3px; background: #fdeef0; cursor: not-allowed; }

.block {
  background: var(--bg, #eee);
  border-left: 3px solid var(--fg, #888);
  border-radius: 6px; padding: 5px 7px; height: 100%;
  display: flex; flex-direction: column; gap: 1px;
  user-select: none; overflow: hidden;
}
.editable .block { cursor: grab; }
.editable .block:active { cursor: grabbing; }
.editable .gridwrap .block:hover { outline: 2px solid var(--primary); outline-offset: -1px; }
.block.dragging { opacity: .45; }
.block.static { cursor: default; }
.b-code { font-weight: 700; color: var(--fg, #333); font-size: 12.5px; }
.b-code .span { font-style: normal; font-size: 10px; opacity: .7; }
.b-sec { font-size: 11.5px; color: #333; display: flex; align-items: center; gap: 3px; }
.b-fac { font-size: 10.5px; color: var(--muted); }
.merged { font-style: normal; }

.hint { color: var(--muted); margin-top: 14px; font-size: 13px; }
.page-h1 { font-size: 20px; margin: 4px 0 6px; }

/* Listings */
table.listing, table.timetable { width: 100%; }
table.listing { border-collapse: collapse; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; overflow: hidden; }
table.listing th, table.listing td { border-bottom: 1px solid var(--line); padding: 9px 12px; text-align: left; font-size: 13px; }
table.listing thead th { background: #fafbfe; color: var(--muted); }
table.listing .mono, .mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
table.listing .reason { color: var(--muted); }

/* Toast */
.toast {
  position: fixed; left: 50%; bottom: 26px; transform: translateX(-50%) translateY(20px);
  background: #222; color: #fff; padding: 11px 16px; border-radius: 10px;
  max-width: 70vw; box-shadow: 0 8px 30px rgba(0,0,0,.25);
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; font-size: 13.5px;
  z-index: 50;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.ok { background: #15663f; }
.toast.warn { background: #8a5a00; }
.toast.bad { background: #98232f; }

/* Modal */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(20, 22, 30, .42);
  display: flex; align-items: center; justify-content: center; z-index: 60; padding: 20px;
}
.modal-overlay[hidden] { display: none; }
.modal {
  background: var(--panel); width: 420px; max-width: 100%; border-radius: 14px;
  box-shadow: 0 24px 70px rgba(0,0,0,.3); overflow: hidden;
  animation: pop .12s ease-out;
}
@keyframes pop { from { transform: translateY(6px) scale(.98); opacity: .6; } to { transform: none; opacity: 1; } }
.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px; border-bottom: 1px solid var(--line); font-size: 15px;
}
.modal-x { border: 0; background: none; font-size: 22px; line-height: 1; color: var(--muted); cursor: pointer; }
.modal-x:hover { color: var(--ink); }
.modal-context { padding: 10px 16px 0; color: var(--muted); font-size: 12.5px; font-family: ui-monospace, Menlo, monospace; }
#modal-body { padding: 12px 16px 4px; display: flex; flex-direction: column; gap: 11px; }
.fld { display: flex; flex-direction: column; gap: 4px; font-size: 12.5px; color: var(--muted); }
.fld input, .fld select {
  font: inherit; font-size: 14px; color: var(--ink); padding: 8px 9px;
  border: 1px solid var(--line); border-radius: 8px; background: #fff;
}
.fld input:focus, .fld select:focus { outline: 2px solid var(--primary); outline-offset: -1px; border-color: var(--primary); }
.reason { margin: 0; font-size: 12px; color: var(--muted); font-style: italic; }
.muted { color: var(--muted); }
.modal-error {
  margin: 4px 16px 0; padding: 9px 11px; background: #fdeef0; color: var(--bad);
  border-radius: 8px; font-size: 12.5px;
}
.modal-foot { display: flex; align-items: center; gap: 8px; padding: 14px 16px; }
.modal-foot .grow { flex: 1; }
.btn {
  font: inherit; font-size: 13px; padding: 8px 14px; border-radius: 8px;
  border: 1px solid var(--line); background: #fff; color: var(--ink); cursor: pointer;
}
.btn:hover { background: #f4f5f9; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { filter: brightness(1.06); background: var(--primary); }
.btn-primary:disabled { opacity: .5; cursor: not-allowed; }
.btn-danger { background: #fff; border-color: #f1c4c9; color: var(--bad); }
.btn-danger:hover { background: #fdeef0; }

/* Week view */
.clear-link { color: var(--primary); font-size: 13px; }
table.weekgrid { min-width: 1040px; table-layout: fixed; }
table.weekgrid .room { width: 13.2%; }
td.weekcell { vertical-align: top; padding: 4px; height: auto; }
.wblock {
  background: var(--bg, #eee); border-left: 3px solid var(--fg, #888);
  border-radius: 6px; padding: 4px 6px; margin-bottom: 4px;
}
.wblock:last-child { margin-bottom: 0; }
.wb-code { display: block; font-weight: 700; font-size: 12px; color: var(--fg, #333); }
.wb-code em { font-style: normal; font-weight: 500; opacity: .7; }
.wb-meta { display: block; font-size: 11px; color: #444; }
.wblock .merged { font-style: normal; }
