* { 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; }
.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; }
#gridArea { flex: 1; min-width: 0; }
#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; }
.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;
}
.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 { margin-left: auto; 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: hidden; min-width: 0;
}
.grid-times { width: 76px; flex-shrink: 0; border-right: 2px solid #555; }
.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: 1; min-width: 0; border-right: 1px solid #999; position: relative; }
.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-shadow: 0 1px 2px rgba(0,0,0,0.25); white-space: pre-line;
}
.sched-block.conflict { outline: 3px solid #ef4444; outline-offset: -1px; }
.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: 30; opacity: 0.85; box-shadow: 0 6px 16px rgba(0,0,0,0.4); }
.sched-block.drag-cross-day { outline: 2px dashed #fff; outline-offset: -2px; }
.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-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; }
.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; }
.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-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; }
  .multi-list { background: #1e2130; border-color: #3a3f55; }
  .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; }
  .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; }
  .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 {
  .no-print, .topbar, #conflictPanel, .filter-bar { display: none !important; }
  body { background: #fff !important; color: #1a1a2e !important; }
  .grid-main, .table-col { background: #fff !important; }
  .sec-table th, .stats-table th { background: #eceef5 !important; }
  #app { padding: 0; }
  .grid-main { border: 1px solid #000; }
  #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; }
}
