:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --panel-2: #f1f3f6;
  --text: #1c2330;
  --muted: #5d6878;
  --border: #e1e5eb;
  --accent: #2f6fde;
  --accent-ink: #ffffff;
  --accent-soft: #e7effc;
  --ok: #1f8a4c;
  --ok-soft: #e3f4ea;
  --warn: #a56400;
  --warn-soft: #fcf1de;
  --bad: #c2352b;
  --bad-soft: #fbe7e5;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.08);
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11151c;
    --panel: #181d26;
    --panel-2: #1f2530;
    --text: #e6e9ef;
    --muted: #9aa4b2;
    --border: #2a3240;
    --accent: #6d9cf0;
    --accent-ink: #0d1320;
    --accent-soft: #1d2a40;
    --ok: #4cc37f;
    --ok-soft: #173326;
    --warn: #e0a548;
    --warn-soft: #362a14;
    --bad: #ef7067;
    --bad-soft: #3a1d1b;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
a { color: var(--accent); }
h1 { font-size: 1.45rem; margin: 0 0 4px; }
h2 { font-size: 1.1rem; margin: 0 0 12px; }
h3 { font-size: 0.95rem; margin: 18px 0 8px; }
p { margin: 0 0 10px; }
code, .mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 0.88em; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.nowrap { white-space: nowrap; }

/* ---------- layout */
.shell { display: grid; grid-template-columns: 230px 1fr; min-height: 100vh; }
.side {
  background: var(--panel);
  border-right: 1px solid var(--border);
  padding: 18px 12px;
  position: sticky;
  top: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 650; padding: 4px 10px 18px; }
.brand svg { color: var(--accent); flex: none; }
.nav a {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border-radius: 8px;
  color: var(--text);
  text-decoration: none;
}
.nav a:hover { background: var(--panel-2); }
.nav a.on { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.side .who { margin-top: auto; padding: 10px; font-size: 0.85rem; color: var(--muted); border-top: 1px solid var(--border); }
.side .who b { color: var(--text); font-weight: 600; display: block; overflow: hidden; text-overflow: ellipsis; }
main { padding: 28px clamp(16px, 4vw, 40px) 110px; max-width: 1180px; width: 100%; min-width: 0; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 20px; }
.page-head p { margin: 0; }

@media (max-width: 860px) {
  .shell { grid-template-columns: minmax(0, 1fr); }
  .side { position: static; height: auto; flex-direction: row; flex-wrap: wrap; align-items: center; padding: 10px 12px; }
  .brand { padding: 4px 8px; }
  .nav { display: flex; flex-wrap: wrap; gap: 2px; width: 100%; }
  .nav a { padding: 6px 10px; }
  .side .who { margin: 0; border: 0; padding: 4px 8px; width: 100%; display: flex; gap: 8px; align-items: center; }
  .side .who b { display: inline; }
}

/* ---------- surfaces */
.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px 20px;
  margin-bottom: 16px;
}
.card.flush { padding: 0; overflow: hidden; }
.grid { display: grid; gap: 16px; }
.grid.two { grid-template-columns: repeat(auto-fit, minmax(min(300px, 100%), 1fr)); }
.grid.stats { grid-template-columns: repeat(auto-fit, minmax(min(140px, 100%), 1fr)); }
.stat { background: var(--panel-2); border-radius: 8px; padding: 12px 14px; }
.stat .n { font-size: 1.6rem; font-weight: 650; line-height: 1.2; }
.stat .l { color: var(--muted); font-size: 0.85rem; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row.spread { justify-content: space-between; }
.stack > * + * { margin-top: 12px; }
hr { border: 0; border-top: 1px solid var(--border); margin: 16px 0; }

/* ---------- controls */
button, .btn {
  font: inherit;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  padding: 7px 14px;
  border-radius: 8px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  text-decoration: none;
  white-space: nowrap;
}
button:hover { background: var(--panel-2); }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
button.primary:hover { filter: brightness(1.06); }
button.danger { color: var(--bad); }
button.danger.solid { background: var(--bad); border-color: var(--bad); color: #fff; }
button.ghost { border-color: transparent; background: transparent; }
button.small { padding: 3px 9px; font-size: 0.85rem; }
button:disabled { opacity: 0.55; cursor: not-allowed; }
button:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible, a:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
input, select, textarea {
  font: inherit;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 10px;
  width: 100%;
  min-width: 0;
}
textarea { min-height: 76px; resize: vertical; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 0.88rem; }
input:disabled, select:disabled, textarea:disabled { background: var(--panel-2); color: var(--muted); }
label.field { display: block; margin-bottom: 14px; }
label.field > span { display: block; font-weight: 600; font-size: 0.9rem; margin-bottom: 4px; }
label.field > small, .hint { display: block; color: var(--muted); font-size: 0.82rem; margin-top: 4px; }
.check { display: flex; align-items: center; gap: 8px; cursor: pointer; }
.check input { width: auto; }
.fields-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(220px, 100%), 1fr)); gap: 0 16px; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 9px; overflow: hidden; flex: none; }
.seg button { border: 0; border-radius: 0; border-right: 1px solid var(--border); }
.seg button:last-child { border-right: 0; }
.seg button.on { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.modes { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(150px, 100%), 1fr)); gap: 10px; margin-bottom: 14px; }
.mode {
  text-align: left;
  display: block;
  white-space: normal;
  padding: 10px 12px;
  border-radius: 9px;
}
.mode b { display: block; }
.mode small { color: var(--muted); }
.mode.on { border-color: var(--accent); background: var(--accent-soft); }

/* ---------- badges, notices */
.badge {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 600;
  background: var(--panel-2);
  color: var(--muted);
  white-space: nowrap;
}
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.bad { background: var(--bad-soft); color: var(--bad); }
.badge.accent { background: var(--accent-soft); color: var(--accent); }
.notice { border-radius: 9px; padding: 10px 14px; margin-bottom: 14px; background: var(--panel-2); }
.notice.ok { background: var(--ok-soft); }
.notice.warn { background: var(--warn-soft); }
.notice.bad { background: var(--bad-soft); }
.notice ul { margin: 6px 0 0; padding-left: 20px; }

/* ---------- tables & lists */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 9px 12px; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.03em; color: var(--muted); font-weight: 600; background: var(--panel-2); }
tr:last-child td { border-bottom: 0; }
td.actions { text-align: right; white-space: nowrap; }
.source {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 14px;
  align-items: center;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
}
.source:last-child { border-bottom: 0; }
.source > div { min-width: 0; }
@media (max-width: 640px) {
  .source { grid-template-columns: auto minmax(0, 1fr); padding: 12px 14px; }
  .source > div:last-child { grid-column: 1 / -1; justify-content: space-between; }
}
.source .title { font-weight: 600; }
.order { display: flex; flex-direction: column; gap: 2px; }
.order button { padding: 0 6px; line-height: 1.2; }
.events { max-height: 640px; overflow: auto; }
.ev { display: grid; grid-template-columns: 92px 1fr; gap: 2px 10px; padding: 7px 14px; border-bottom: 1px solid var(--border); font-size: 0.9rem; }
.ev .d { color: var(--muted); font-variant-numeric: tabular-nums; }
.ev .why { grid-column: 2; font-size: 0.8rem; }
.ev.keep .why { color: var(--ok); }
.ev.drop .why { color: var(--bad); }
.ev.drop .t { color: var(--muted); }
.course {
  display: grid;
  grid-template-columns: 1.4fr 1fr 80px auto;
  grid-template-areas: "course teacher block x" "match exclude exclude .";
  gap: 6px 8px;
  padding: 10px;
  margin-bottom: 8px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--panel-2);
}
.course label { display: block; font-size: 0.75rem; color: var(--muted); font-weight: 600; }
.course .c-course { grid-area: course; } .course .c-teacher { grid-area: teacher; } .course .c-block { grid-area: block; }
.course .c-match { grid-area: match; } .course .c-exclude { grid-area: exclude; } .course .c-x { grid-area: x; align-self: end; }
@media (max-width: 560px) {
  .course { grid-template-columns: 1fr 1fr; grid-template-areas: "course course" "teacher block" "match exclude" "x x"; }
}
.editor { display: grid; grid-template-columns: minmax(0, 1.15fr) minmax(0, 1fr); gap: 16px; align-items: start; }
.editor .sticky { position: sticky; top: 16px; }
@media (max-width: 1000px) {
  .editor { grid-template-columns: 1fr; }
  .editor .sticky { position: static; }
}

/* ---------- save bar, toasts, modal */
.savebar {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(16px + env(safe-area-inset-bottom, 0px));
  background: var(--text);
  color: var(--bg);
  padding: 10px 12px 10px 18px;
  border-radius: 12px;
  display: flex;
  gap: 12px;
  align-items: center;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.25);
  z-index: 20;
  max-width: calc(100vw - 32px);
}
.savebar button { border-color: transparent; }
.toasts { position: fixed; top: 16px; right: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 30; }
.toast { background: var(--panel); border: 1px solid var(--border); border-left: 4px solid var(--accent); box-shadow: 0 6px 24px rgba(0,0,0,.15); padding: 10px 14px; border-radius: 8px; max-width: 380px; }
.toast.bad { border-left-color: var(--bad); }
.toast.ok { border-left-color: var(--ok); }
.modal-bg { position: fixed; inset: 0; background: rgba(10, 14, 20, 0.5); display: grid; place-items: center; z-index: 40; padding: 16px; }
.modal { background: var(--panel); border-radius: 12px; padding: 20px 22px; width: min(620px, 100%); max-height: 88vh; overflow: auto; }
.center { min-height: 70vh; display: grid; place-items: center; text-align: center; }
.spinner { width: 22px; height: 22px; border-radius: 50%; border: 3px solid var(--border); border-top-color: var(--accent); animation: spin 0.8s linear infinite; display: inline-block; vertical-align: middle; }
@keyframes spin { to { transform: rotate(360deg); } }
.empty { padding: 28px; text-align: center; color: var(--muted); }

/* ---------- calendar tab */
:root {
  --c0: #2f6fde; --c1: #1f8a4c; --c2: #b4531f; --c3: #8a3fc2; --c4: #0f7f8c; --c5: #c2367a; --c6: #7a6a12; --c7: #4d5bd6;
  --own: #5d6878;
}
@media (prefers-color-scheme: dark) {
  :root { --c0: #6d9cf0; --c1: #4cc37f; --c2: #f0935a; --c3: #c08af0; --c4: #45c2cf; --c5: #f07ab3; --c6: #d6c35a; --c7: #8d97f5; --own: #9aa4b2; }
}
.c0 { --c: var(--c0); } .c1 { --c: var(--c1); } .c2 { --c: var(--c2); } .c3 { --c: var(--c3); }
.c4 { --c: var(--c4); } .c5 { --c: var(--c5); } .c6 { --c: var(--c6); } .c7 { --c: var(--c7); } .own { --c: var(--own); }

.cal-toolbar { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.legend { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.legend-item { padding: 3px 10px; font-size: 0.85rem; border-radius: 999px; }
.legend-item .sw { width: 10px; height: 10px; border-radius: 3px; background: var(--c); display: inline-block; }
.legend-item.off { opacity: 0.45; text-decoration: line-through; }
.legend-badge { background: color-mix(in srgb, var(--c) 18%, transparent); color: var(--c); }

.month-wrap { overflow-x: auto; }
.month { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); min-width: 620px; }
.dow { padding: 8px; font-size: 0.75rem; font-weight: 600; text-transform: uppercase; color: var(--muted); background: var(--panel-2); border-bottom: 1px solid var(--border); }
.day { min-height: 112px; padding: 4px 4px 6px; border-right: 1px solid var(--border); border-bottom: 1px solid var(--border); display: flex; flex-direction: column; gap: 2px; min-width: 0; cursor: default; }
.day:nth-child(7n) { border-right: 0; }
.day.other { background: var(--panel-2); }
.day.other .num, .day.other .chip { opacity: 0.55; }
.day .num { font-size: 0.8rem; font-weight: 600; width: 24px; height: 24px; display: grid; place-items: center; border-radius: 50%; margin-bottom: 2px; }
.day.today .num { background: var(--accent); color: var(--accent-ink); }
.chip {
  display: block; width: 100%; text-align: left; padding: 1px 6px; font-size: 0.76rem; line-height: 1.45;
  border: 0; border-left: 3px solid var(--c); border-radius: 4px;
  background: color-mix(in srgb, var(--c) 14%, transparent); color: var(--text);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.chip:hover { background: color-mix(in srgb, var(--c) 26%, transparent); }
.chip .t { color: var(--muted); margin-right: 4px; font-variant-numeric: tabular-nums; }
.more { border: 0; background: none; padding: 0 6px; font-size: 0.75rem; color: var(--accent); text-align: left; }

.agenda-day { display: grid; grid-template-columns: 76px 1fr; gap: 12px; padding: 10px 16px; border-bottom: 1px solid var(--border); }
.agenda-day:last-child { border-bottom: 0; }
.agenda-date { display: flex; flex-direction: column; font-size: 0.85rem; color: var(--muted); }
.agenda-date b { color: var(--text); font-size: 0.95rem; }
.agenda-date.today b { color: var(--accent); }
.agenda-events { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.agenda-ev {
  display: grid; grid-template-columns: minmax(64px, max-content) 1fr auto; gap: 10px; align-items: center; text-align: left; white-space: normal;
  width: 100%; border: 0; border-left: 4px solid var(--c); background: color-mix(in srgb, var(--c) 9%, transparent);
  border-radius: 6px; padding: 6px 10px;
}
.agenda-ev:hover { background: color-mix(in srgb, var(--c) 18%, transparent); }
.agenda-ev .when { font-size: 0.82rem; color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }
.agenda-ev .what { min-width: 0; overflow-wrap: anywhere; }
@media (max-width: 560px) {
  .agenda-day { grid-template-columns: 1fr; gap: 4px; }
  .agenda-date { flex-direction: row; gap: 6px; }
  .agenda-ev { grid-template-columns: minmax(58px, max-content) 1fr; }
  .agenda-ev .badge { display: none; }
}

/* ---------- typed-in dates */
.mdate {
  display: grid; grid-template-columns: 150px 150px 132px 132px 1fr auto;
  grid-template-areas: "date end time etime title x" "notes notes notes notes notes .";
  gap: 6px 8px; padding: 10px; margin-bottom: 8px; border: 1px solid var(--border); border-radius: 9px; background: var(--panel-2);
}
.mdate label { display: block; font-size: 0.75rem; color: var(--muted); font-weight: 600; }
.mdate .m-date { grid-area: date; } .mdate .m-end { grid-area: end; } .mdate .m-time { grid-area: time; }
.mdate .m-etime { grid-area: etime; } .mdate .m-title { grid-area: title; min-width: 0; } .mdate .m-notes { grid-area: notes; }
.mdate .m-x { grid-area: x; align-self: end; }
@media (max-width: 900px) {
  .mdate { grid-template-columns: 1fr 1fr; grid-template-areas: "title title" "date end" "time etime" "notes notes" "x x"; }
}
