* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  font-size: 14px;
  color: #1a1a2e;
  background: #f4f5f9;
}
button { font: inherit; cursor: pointer; }
input, select { font: inherit; padding: 3px 6px; border: 1px solid #c6c9d4; border-radius: 4px; }
.spacer { flex: 1; }

/* ---------- top bar ---------- */
.topbar {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  background: #1f2440; color: #fff; padding: 8px 14px;
  position: sticky; top: 0; z-index: 50;
}
.topbar h1 { font-size: 17px; margin: 0 10px 0 0; white-space: nowrap; }
.topbar select { max-width: 220px; }
.btn {
  border: 1px solid #c6c9d4; border-radius: 5px; background: #fff; padding: 5px 12px;
}
.btn.small { padding: 3px 9px; font-size: 13px; }
.btn.primary { background: #2563eb; border-color: #2563eb; color: #fff; }
.btn.danger { background: #fff; border-color: #d33; color: #b22; }
.btn:hover { filter: brightness(0.96); }
#editToggle.active { background: #fbbf24; border-color: #fbbf24; color: #1f2440; font-weight: 600; }
.badge { padding: 2px 8px; border-radius: 10px; font-size: 11px; font-weight: 700; }
.badge-published { background: #16a34a; color: #fff; }
.badge-draft { background: #fbbf24; color: #1f2440; }
.badge-archived { background: #9ca3af; color: #fff; }
.readonly-note { color: #fde68a; font-size: 12px; font-weight: 600; }
.save-state { font-size: 12px; opacity: 0.85; }
.save-state.error { color: #fca5a5; font-weight: 700; }
.save-state.dirty, .save-state.saving { color: #fde68a; }

/* ---------- layout ---------- */
#app { padding: 10px 14px 40px; }
main { display: flex; gap: 12px; align-items: flex-start; }
.left-col { flex: 1; min-width: 0; }
#gridArea { min-width: 0; position: relative; }
.hl-clear {
  position: absolute; top: 2px; right: 8px; z-index: 45; background: #111; color: #fff;
  border: none; box-shadow: 0 2px 6px rgba(0,0,0,0.3);
}
#conflictPanel {
  width: 320px; flex-shrink: 0; background: #fff; border: 1px solid #dde0ea;
  border-radius: 8px; padding: 10px; max-height: 80vh; overflow: auto;
  position: sticky; top: 52px;
}
.empty-state { padding: 60px 20px; text-align: center; color: #666; background: #fff; border-radius: 8px; }

/* ---------- filter bar ---------- */
.filter-bar {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  background: #fff; border: 1px solid #dde0ea; border-radius: 8px;
  padding: 7px 10px; margin-bottom: 10px; font-size: 13px;
}
.filter-bar input[type="search"] { width: 140px; }
.filter-bar .chk { display: inline-flex; align-items: center; gap: 4px; cursor: pointer; }
.numbering-controls { display: inline-flex; align-items: center; gap: 6px; margin-left: auto; }
.numbering-controls .seg button { padding: 4px 8px; }
.numbering-readonly { margin-left: auto; color: #777; }
.sugg, .sugg-hint { color: #777; font-size: 0.85em; font-weight: 400; }
.new-section {
  display: inline-block; border-radius: 3px; background: #eef2ff; color: #4b5563;
  padding: 1px 4px; font-size: 10px; font-weight: 600; text-transform: uppercase;
}
.multi { position: relative; }
.multi summary {
  list-style: none; cursor: pointer; border: 1px solid #c6c9d4; border-radius: 4px;
  padding: 3px 8px; background: #fff; user-select: none;
}
.multi[open] summary { background: #eef2ff; }
.multi-list {
  position: absolute; z-index: 40; top: 110%; left: 0; background: #fff;
  border: 1px solid #c6c9d4; border-radius: 6px; padding: 6px 10px;
  max-height: 260px; overflow: auto; box-shadow: 0 6px 18px rgba(0,0,0,0.15);
  display: flex; flex-direction: column; gap: 2px; min-width: 170px;
}
.multi-actions {
  position: sticky; top: -6px; display: flex; gap: 8px; align-items: center;
  margin: -6px -10px 4px; padding: 6px 10px; background: inherit;
  border-bottom: 1px solid #e2e4ee;
}
.multi-done {
  margin-left: auto; border: 1px solid #c6c9d4; border-radius: 4px; padding: 1px 10px;
  background: #eef2ff; cursor: pointer; font-weight: 600;
}
.day-chip {
  display: inline-block; border: 1px solid #c6c9d4; border-radius: 4px;
  padding: 2px 7px; margin-right: 2px; cursor: pointer; user-select: none;
}
.day-chip.on { background: #2563eb; color: #fff; border-color: #2563eb; }
.day-chip input { display: none; }
.link-btn { background: none; border: none; color: #2563eb; padding: 2px 4px; text-decoration: underline; font-size: 13px; }
.link-btn:disabled { color: #aaa; text-decoration: none; }
.preset-box { display: flex; align-items: center; gap: 2px; }

/* ---------- grid ---------- */
.grid-wrap { display: flex; gap: 8px; }
.grid-legend {
  width: 108px; flex-shrink: 0; display: flex; flex-direction: column; gap: 4px;
  padding-top: 30px;
}
.legend-title { font-weight: 700; font-size: 12px; text-align: center; }
.legend-item {
  color: #fff; font-size: 12px; font-weight: 600; text-align: center;
  border-radius: 4px; padding: 4px 2px; cursor: pointer;
}
.grid-main {
  flex: 1; display: flex; background: #fff; border: 1px solid #b8bcc9;
  border-radius: 8px; overflow-x: auto; overflow-y: hidden; min-width: 0;
  isolation: isolate;
}
.grid-times {
  width: 76px; flex-shrink: 0; border-right: 2px solid #555;
  position: sticky; left: 0; z-index: 50; background: #fff;
}
.grid-head {
  height: 28px; line-height: 28px; text-align: center; font-weight: 700;
  background: #eceef5; border-bottom: 2px solid #555;
}
.time-cell {
  height: 26px; font-size: 11px; text-align: right; padding-right: 6px;
  color: #444; border-top: 1px solid transparent;
}
.time-cell.hour { border-top: 1px solid #ccc; font-weight: 600; }
.grid-times .time-cell:first-of-type { border-top: none; }
.grid-day { flex: 0 0 auto; min-width: 0; border-right: 1px solid #999; position: relative; overflow: visible; }
.grid-day:last-child { border-right: none; }
.grid-day:nth-child(odd) .day-body { background: #f7f8fc; }
.day-body { position: relative; }
.slot-line { position: absolute; left: 0; right: 0; height: 0; border-top: 1px dotted #e2e4ee; }
.slot-line.hour { border-top: 1px solid #d4d7e3; }
.std-band {
  position: absolute; left: 0; right: 0; box-sizing: border-box;
  border-top: 1px dashed #b8bdd1; border-bottom: 1px dashed #b8bdd1;
  background: rgba(90, 110, 180, 0.06); pointer-events: none;
}
.std-band span {
  position: absolute; top: 0; left: 2px; font-size: 9px; line-height: 1; color: #8a90a8;
}
.sched-block {
  position: absolute; color: #fff; font-size: 11px; font-weight: 600; line-height: 1.25;
  border-radius: 4px; display: flex; flex-direction: column; align-items: center;
  justify-content: center; text-align: center; overflow: hidden; cursor: pointer;
  box-sizing: border-box; box-shadow: 0 1px 2px rgba(0,0,0,0.25); white-space: pre-line;
}
.sched-block .block-course, .sched-block .block-instructor {
  display: block; max-width: 100%; overflow: hidden; text-overflow: ellipsis;
}
.sched-block .block-course { font-weight: 800; }
.sched-block .block-instructor { font-size: 0.9em; opacity: 0.92; }
.sched-block .block-room {
  display: block; max-width: 100%; font-size: 0.82em; font-weight: 600; opacity: 0.85;
  line-height: 1.1; white-space: nowrap; overflow: hidden; text-overflow: clip;
}
.sched-block:hover { z-index: 40; filter: brightness(1.12); }
.sched-block.outside, .legend-outside {
  border: 1px dashed rgba(255,255,255,.75);
  background-image: repeating-linear-gradient(45deg, rgba(0,0,0,.18) 0 4px, transparent 4px 9px);
}
.sched-block.outside.conflict {
  background-image: repeating-linear-gradient(45deg, rgba(0,0,0,.18) 0 4px, transparent 4px 9px),
    repeating-linear-gradient(135deg, rgba(255,255,255,.30) 0 7px, transparent 7px 14px);
}
.sched-block.conflict {
  outline: 3px solid #ef4444; outline-offset: -1px;
  background-image: repeating-linear-gradient(135deg, rgba(255,255,255,.30) 0 7px, transparent 7px 14px);
}
.sched-block.conflict.dark-text {
  background-image: repeating-linear-gradient(135deg, rgba(0,0,0,.18) 0 7px, transparent 7px 14px);
}
.sched-block.highlight { outline: 3px solid #111; outline-offset: -1px; z-index: 5; }
.sched-block.dimmed { opacity: 0.22; }
.sched-block.draggable { cursor: grab; }
.sched-block.dragging {
  cursor: grabbing; z-index: 60; opacity: 0.85; pointer-events: none;
  box-shadow: 0 6px 16px rgba(0,0,0,0.4);
}
.sched-block.drag-cross-day { outline: 2px dashed #fff; outline-offset: -2px; }
.drop-target {
  position: absolute; box-sizing: border-box; border-radius: 4px; pointer-events: none; z-index: 45;
  border: 2px dashed #2563eb; background: rgba(37, 99, 235, 0.18);
}
.online-strip {
  display: flex; flex-wrap: wrap; align-items: center; gap: 4px; margin-top: 8px; padding: 6px 8px;
  border: 1px dashed var(--border, #888); border-radius: 6px;
}
.online-title { font-size: .85em; opacity: .75; margin-right: 6px; }
.online-block {
  position: static; height: auto; width: auto; min-width: 64px; padding: 2px 6px; cursor: pointer;
}
.drag-tip {
  position: absolute; z-index: 60; background: #1f2440; color: #fff; font-size: 12px;
  padding: 3px 8px; border-radius: 4px; pointer-events: none; white-space: nowrap;
}
.grid-wrap { position: relative; }

/* ---------- conflicts ---------- */
.conflict-head { display: flex; align-items: center; gap: 6px; margin-bottom: 8px; flex-wrap: wrap; }
.pill { border-radius: 10px; padding: 2px 9px; font-size: 12px; font-weight: 700; color: #fff; }
.pill-err { background: #dc2626; }
.pill-warn { background: #d97706; }
.pill-info { background: #2563eb; }
.conflict-none { color: #16a34a; font-weight: 600; }
.conflict-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 5px; }
.conflict-list li {
  padding: 6px 8px; border-radius: 5px; font-size: 12.5px; cursor: pointer;
  border-left: 4px solid transparent; background: #f7f7fa;
}
.conflict-list li:hover { filter: brightness(0.95); }
.c-error { border-left-color: #dc2626 !important; background: #fef2f2 !important; }
.c-warning { border-left-color: #d97706 !important; background: #fffbeb !important; }
.c-info { border-left-color: #2563eb !important; background: #eff6ff !important; }

/* ---------- tables ---------- */
#tableArea { margin-top: 18px; }
.table-head { display: flex; align-items: center; gap: 14px; margin-bottom: 8px; }
.table-head h2 { margin: 0; font-size: 16px; }
.seg { display: inline-flex; border: 1px solid #c6c9d4; border-radius: 6px; overflow: hidden; }
.seg button { border: none; background: #fff; padding: 4px 12px; font-size: 13px; }
.seg button.on { background: #2563eb; color: #fff; }
.tables-flex { display: flex; gap: 16px; align-items: flex-start; flex-wrap: wrap; }
.table-col { background: #fff; border: 1px solid #dde0ea; border-radius: 8px; padding: 10px; }
.table-col h3 { margin: 0 0 8px; font-size: 13px; color: #555; }
.stats-col { min-width: 260px; }
table.sec-table, table.stats-table, table.roster-table, table.rules-table { border-collapse: collapse; font-size: 13px; }
.sec-table th, .sec-table td, .stats-table th, .stats-table td,
.roster-table th, .roster-table td, .rules-table th, .rules-table td {
  border: 1px solid #e2e4ee; padding: 3px 8px; text-align: left;
}
.sec-table th, .stats-table th, .roster-table th, .rules-table th { background: #eceef5; }
.rules-table input[type=number] { width: 4.5em; }
.rules-table input { max-width: 14em; }
.sec-table tbody tr { cursor: pointer; }
.sec-table tbody tr:hover { background: #f0f4ff; }
.row-bad { background: #fef2f2 !important; }
.row-bad td:first-child { border-left: 4px solid #dc2626; }
.row-elim { color: #999; text-decoration: line-through; }
.row-outside { color: #6b7280; font-style: italic; }
.tag-outside {
  display: inline-block; font-size: 10px; font-style: normal; font-weight: 600; line-height: 1.3;
  padding: 0 4px; border: 1px solid currentColor; border-radius: 3px; vertical-align: 1px; opacity: .8;
}
.chk-label { flex-direction: column; }
.chk-label small { color: #777; font-weight: 400; }
.row-actions { white-space: nowrap; }
.stats-table .under { color: #dc2626; font-weight: 700; }
.stats-table .over { color: #2563eb; }
.stats-table .exact { color: #16a34a; }
.stats-table .notarget { color: #999; }
.stats-table .notes { font-size: 12px; font-style: italic; color: #555; max-width: 220px; }
.stats-table small { color: #888; }
.partner-head {
  position: relative;
  display: inline-block;
  white-space: nowrap;
}
.partner-head .partner-pick {
  position: absolute;
  inset: 0;
  opacity: 0;
  width: 100%;
  height: 100%;
  cursor: pointer;
  margin: 0;
}
.partner-caret {
  font-size: 10px;
  color: #888;
  cursor: pointer;
}
.roster-table input[type=number] { width: 4.5em; }
.roster-table .roster-notes { width: 150px; }
.roster-table .computed { color: #888; font-style: italic; }
@media (prefers-color-scheme: dark) { .stats-table .notes { color: #aab; } }
.total-row { font-weight: 700; background: #eceef5; }

/* ---------- modals ---------- */
.overlay {
  position: fixed; inset: 0; background: rgba(15,18,35,0.45); z-index: 100;
  display: flex; align-items: flex-start; justify-content: center; padding: 40px 16px; overflow: auto;
}
.modal {
  background: #fff; border-radius: 10px; padding: 18px 22px; width: 100%;
  max-width: 720px; box-shadow: 0 20px 60px rgba(0,0,0,0.3);
}
.modal h2 { margin-top: 0; font-size: 17px; }
.modal h3 { font-size: 14px; margin: 14px 0 6px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 14px; }
.form-grid label { display: flex; flex-direction: column; font-size: 12.5px; color: #444; gap: 2px; }
.form-grid label.wide { grid-column: 1 / -1; }
.select-other { display: flex; gap: 4px; }
.select-other select { flex: 1; min-width: 0; }
.other-input.hidden { display: none; }
.modal-actions { display: flex; align-items: center; gap: 8px; margin-top: 16px; }
.hint { font-size: 12.5px; color: #666; }
.imp-scroll { max-height: 320px; overflow: auto; border: 1px solid #e2e4ee; border-radius: 6px; margin-top: 8px; }
.imp-controls { display: flex; flex-wrap: wrap; gap: 6px 18px; align-items: center; margin-top: 8px; font-size: 13px; }
.imp-controls label, .imp-subjects { display: inline-flex; align-items: center; gap: 6px; }
.imp-subjects label { gap: 3px; }
.imp-warnings { color: #b45309; font-size: 13px; }
.imp-err { color: #b22; }
.pair-chip { display: inline-flex; align-items: center; gap: 3px; border: 1px solid #dde0ea; border-radius: 6px; padding: 3px 6px; margin: 2px; }

/* ---------- toast ---------- */
.toast {
  position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%);
  background: #1f2440; color: #fff; padding: 9px 18px; border-radius: 8px;
  z-index: 200; box-shadow: 0 6px 18px rgba(0,0,0,0.3);
}
.toast.error { background: #b91c1c; }

/* ---------- dark mode ---------- */
@media (prefers-color-scheme: dark) {
  body { background: #14161f; color: #e4e6ef; }
  input, select { background: #1e2130; color: #e4e6ef; border-color: #3a3f55; }
  .btn { background: #2a2f45; border-color: #3a3f55; color: #e4e6ef; }
  .btn.primary { background: #2563eb; border-color: #2563eb; color: #fff; }
  .btn.danger { background: #2a2f45; border-color: #7f1d1d; color: #f87171; }
  .btn:hover { filter: brightness(1.15); }
  .topbar { background: #0d0f1a; }
  #conflictPanel, .filter-bar, .table-col, .empty-state {
    background: #1e2130; border-color: #33384d;
  }
  .empty-state { color: #9aa0b5; }
  .multi summary { background: #2a2f45; border-color: #3a3f55; }
  .multi[open] summary { background: #253055; }
  .numbering-readonly, .sugg, .sugg-hint { color: #9aa0b5; }
  .new-section { background: #2a2f45; color: #b6bccd; }
  .multi-list { background: #1e2130; border-color: #3a3f55; }
  .multi-actions { border-bottom-color: #3a3f55; }
  .multi-done { background: #253055; border-color: #3a3f55; color: inherit; }
  .day-chip { border-color: #3a3f55; }
  .link-btn { color: #7ea6ff; }
  .link-btn:disabled { color: #555c75; }
  .grid-main { background: #1e2130; border-color: #3a3f55; }
  .grid-head { background: #272c40; border-bottom-color: #4a5069; }
  .grid-times { border-right-color: #4a5069; background: #1e2130; }
  .time-cell { color: #9aa0b5; }
  .time-cell.hour { border-top-color: #3a3f55; }
  .grid-day { border-right-color: #3a3f55; }
  .grid-day:nth-child(odd) .day-body { background: #22263a; }
  .slot-line { border-top-color: #2a2f45; }
  .slot-line.hour { border-top-color: #33384d; }
  .std-band { border-color: #4a5069; background: rgba(140, 160, 230, 0.07); }
  .std-band span { color: #7d849c; }
  .sched-block.highlight { outline-color: #fff; }
  .conflict-list li { background: #262b40; }
  .c-error { background: #3a1e22 !important; }
  .c-warning { background: #3a2f1a !important; }
  .c-info { background: #1c2a45 !important; }
  .seg { border-color: #3a3f55; }
  .seg button { background: #2a2f45; color: #e4e6ef; }
  .seg button.on { background: #2563eb; color: #fff; }
  .table-col h3 { color: #9aa0b5; }
  .sec-table th, .sec-table td, .stats-table th, .stats-table td,
  .roster-table th, .roster-table td, .rules-table th, .rules-table td { border-color: #33384d; }
  .sec-table th, .stats-table th, .roster-table th, .rules-table th { background: #272c40; }
  .sec-table tbody tr:hover { background: #253055; }
  .row-bad { background: #3a1e22 !important; }
  .row-elim { color: #666e88; }
  .row-outside { color: #8b93a8; }
  .stats-table .under { color: #f87171; }
  .stats-table .over { color: #7ea6ff; }
  .stats-table .exact { color: #4ade80; }
  .stats-table .notarget { color: #666e88; }
  .total-row { background: #272c40; }
  .modal { background: #1e2130; }
  .form-grid label { color: #b6bccd; }
  .hint { color: #9aa0b5; }
  .imp-scroll { border-color: #33384d; }
  .imp-warnings { color: #fbbf24; }
  .imp-err { color: #f87171; }
  .pair-chip { border-color: #3a3f55; }
  .conflict-none { color: #4ade80; }
  .badge-draft { color: #1f2440; }
}

/* ---------- print ---------- */
@media print {
  @page { size: landscape; margin: 10mm; }
  .no-print, .topbar, #conflictPanel, .filter-bar { display: none !important; }
  body { background: #fff !important; color: #1a1a2e !important; }
  main { display: block; }
  .grid-wrap { flex-direction: column; break-inside: avoid; }
  .grid-legend {
    width: auto; flex-direction: row; flex-wrap: wrap; gap: 4px; max-height: none;
  }
  .legend-title { width: 100%; }
  .grid-main, .table-col { background: #fff !important; }
  .sec-table th, .stats-table th { background: #eceef5 !important; }
  #app { padding: 0; }
  .grid-main { overflow: visible !important; border: 1px solid #000; break-inside: avoid; }
  .online-strip { break-inside: avoid; }
  .row-actions, .seg, #addSection { display: none !important; }
  #tableArea { page-break-before: always; }
  .sched-block { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .legend-item { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}

@media (max-width: 900px) {
  main { flex-direction: column; }
  #conflictPanel { width: 100%; position: static; max-height: none; }
}
