:root {
  --bg: #fafafa;
  --card: #ffffff;
  --ink: #111111;
  --ink-soft: #555555;
  --line: #111111;
  --line-soft: #e2e2e2;
  --warn: #b45309;
  --warn-bg: #fef3c7;
  --bad: #b91c1c;
  --bad-bg: #fde8e8;
  --good: #166534;
  --good-bg: #e7f5ea;
  --sel: #f0f0f0;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0; padding: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang TC",
    "Microsoft JhengHei", "Noto Sans TC", sans-serif;
  font-size: 14px;
  line-height: 1.5;
}

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

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 40;
  background: #fff;
  border-bottom: 2px solid var(--line);
}
.topbar-inner {
  max-width: 1280px; margin: 0 auto; padding: 10px 16px;
  display: flex; align-items: center; gap: 10px;
}
.brand {
  background: #fff; color: var(--ink);
  border: 2px solid var(--line);
  font-weight: 800; font-size: 15px; letter-spacing: .4px;
  padding: 7px 12px; cursor: pointer;
  border-radius: 2px;
}
.brand:hover { background: var(--ink); color: #fff; }
.topbar-spacer { flex: 1; }
.topnav { display: flex; gap: 8px; flex-wrap: wrap; }

/* ---------- Buttons ---------- */
.btn {
  appearance: none; border: 2px solid var(--line);
  background: #fff; color: var(--ink);
  padding: 6px 12px; font-size: 13px; font-weight: 600;
  cursor: pointer; border-radius: 2px; line-height: 1.2;
  display: inline-flex; align-items: center; gap: 5px;
  transition: background .12s, color .12s;
  font-family: inherit;
}
.btn:hover { background: var(--sel); }
.btn:active { transform: translateY(1px); }
.btn-black { background: var(--ink); color: #fff; }
.btn-black:hover { background: #333; color: #fff; }
.btn-ghost { border-color: var(--line-soft); background: #fff; }
.btn-ghost:hover { border-color: var(--line); background: var(--sel); }
.btn-danger { background: var(--bad); border-color: var(--bad); color: #fff; }
.btn-danger:hover { background: #7f1d1d; color: #fff; }
.btn-sm { padding: 3px 8px; font-size: 12px; }
.btn:disabled { opacity: .45; cursor: not-allowed; }

.icon-btn {
  appearance: none; border: 1px solid var(--line-soft);
  background: #fff; color: var(--ink);
  width: 26px; height: 26px; font-size: 13px; line-height: 1;
  cursor: pointer; border-radius: 2px;
  display: inline-flex; align-items: center; justify-content: center;
}
.icon-btn:hover { border-color: var(--line); background: var(--sel); }

/* ---------- Crumb ---------- */
.crumb {
  max-width: 1280px; margin: 0 auto; padding: 10px 16px 0; width: 100%;
  display: flex; align-items: center; gap: 6px; flex-wrap: wrap; font-size: 13px;
}
.crumb a { color: var(--ink); font-weight: 600; text-decoration: none; padding: 2px 4px; }
.crumb a:hover { text-decoration: underline; }
.crumb .crumb-sep { color: #aaa; }
.crumb .crumb-cur { font-weight: 800; }

/* ---------- View ---------- */
.view {
  max-width: 1280px; width: 100%; margin: 0 auto; padding: 14px 16px 30px;
  outline: none; flex: 1;
}

/* ---------- Hero / stats ---------- */
.hero { margin-bottom: 18px; }
.hero h1 { font-size: 24px; font-weight: 800; margin: 4px 0 2px; letter-spacing: .3px; }
.hero .sub { color: var(--ink-soft); margin: 0; }
.stat-grid {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 10px; margin: 14px 0;
}
.stat-card {
  background: var(--card); border: 1px solid var(--line);
  border-top-width: 4px; padding: 10px 14px; border-radius: 2px;
}
.stat-card .num { font-size: 26px; font-weight: 800; font-family: var(--mono); }
.stat-card .lab { color: var(--ink-soft); font-size: 12px; margin-top: 2px; }

.bar {
  height: 10px; background: #eee; border: 1px solid var(--line-soft);
  position: relative; overflow: hidden; border-radius: 2px;
}
.bar > .fill { display: block; height: 100%; background: var(--ink); transition: width .3s; }

/* ---------- Panels & cards ---------- */
.panel {
  background: var(--card); border: 1px solid var(--line-soft);
  border-radius: 3px; margin-bottom: 16px; overflow: hidden;
}
.panel-head {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 10px 14px; border-bottom: 1px solid var(--line-soft);
  background: #fff;
}
.panel-title { font-weight: 800; font-size: 15px; flex: 1; }
.panel-body { padding: 14px; }

.subject-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(340px, 1fr)); gap: 14px; }
.subject-card {
  background: var(--card); border: 1px solid var(--line);
  border-radius: 2px; padding: 0; display: flex; flex-direction: column;
}
.subject-card-head {
  padding: 12px 14px; border-bottom: 1px solid var(--line-soft);
  display: flex; align-items: center; gap: 8px; cursor: pointer;
}
.subject-card-head:hover { background: var(--sel); }
.subject-card-head .name { font-size: 17px; font-weight: 800; flex: 1; }
.subject-card-body { padding: 8px 14px 14px; }

.chip {
  display: inline-flex; align-items: center; gap: 4px;
  border: 1px solid var(--line); padding: 1px 7px; font-size: 12px;
  border-radius: 2px; background: #fff; font-family: var(--mono);
}
.chip-bad { background: var(--bad-bg); border-color: var(--bad); color: var(--bad); }
.chip-good { background: var(--good-bg); border-color: var(--good); color: var(--good); }
.chip-warn { background: var(--warn-bg); border-color: var(--warn); color: var(--warn); }
.chip-ghost { border-color: var(--line-soft); color: var(--ink-soft); }

.chapter-row {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  padding: 8px 6px; border-top: 1px solid var(--line-soft);
}
.chapter-row:first-of-type { border-top: none; }
.chapter-row .cname { font-weight: 700; cursor: pointer; min-width: 120px; }
.chapter-row .cname:hover { text-decoration: underline; }
.chapter-row .meta { color: var(--ink-soft); font-size: 12px; font-family: var(--mono); }

.name-list { font-size: 12px; color: var(--ink-soft); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }

.empty {
  text-align: center; padding: 40px 20px; color: var(--ink-soft);
}
.empty .empty-ico { font-size: 34px; margin-bottom: 8px; }

/* ---------- Toolbar ---------- */
.toolbar {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  margin-bottom: 12px;
}
.toolbar .tb-title { font-size: 18px; font-weight: 800; margin-right: 4px; }
.toolbar .grow { flex: 1; }
.searchbox {
  display: flex; align-items: center; gap: 6px;
  border: 1px solid var(--line); background: #fff; padding: 3px 8px; border-radius: 2px;
}
.searchbox input { border: none; outline: none; font-size: 13px; width: 130px; font-family: inherit; }

/* ---------- Spreadsheet ---------- */
.table-scroller {
  overflow: auto; max-height: calc(100vh - 300px); min-height: 200px;
  border: 1px solid var(--line); background: #fff; border-radius: 2px;
}
.grade-table { border-collapse: separate; border-spacing: 0; min-width: 100%; }
.grade-table th, .grade-table td {
  border-right: 1px solid var(--line-soft); border-bottom: 1px solid var(--line-soft);
  padding: 0; text-align: center; white-space: nowrap;
}
.grade-table th { background: #fff; position: sticky; z-index: 5; }
.grade-table thead th { top: 0; border-bottom: 2px solid var(--line); }
.grade-table col.studname { position: sticky; left: 0; z-index: 6; background: #fff; }
.grade-table th.studname, .grade-table td.studname {
  position: sticky; left: 0; background: #fff; z-index: 7; min-width: 110px; max-width: 180px;
}
.grade-table thead th.studname { z-index: 9; }
.grade-table td.avg-col { background: #f7f7f7; font-family: var(--mono); font-weight: 700; }
.grade-table th.avg-col { background: #f7f7f7; }
.grade-table th .col-name { font-weight: 800; font-size: 12px; padding: 6px 8px; display: block; }
.grade-table th .col-sub { font-size: 10px; color: var(--ink-soft); padding: 0 6px 5px; display: block; font-family: var(--mono); }
.grade-table td .cell {
  display: block; min-width: 56px; height: 100px; /* fallback */
  height: auto; padding: 6px 8px; font-family: var(--mono); font-size: 13px; cursor: pointer;
}
.grade-table td.missing { background: var(--warn-bg); }
.grade-table td.fail { background: var(--bad-bg); color: var(--bad); }
.grade-table td.ok { background: #fff; }
.grade-table td .cell:hover { outline: 1px solid var(--line); outline-offset: -1px; }
.grade-table td .cell input, .grade-table td .cell .cellinput {
  width: 100%; border: 2px solid var(--line); outline: none; font-family: var(--mono);
  font-size: 13px; padding: 2px 4px; text-align: center; background:#fff;
}
.grade-table tr.row-focus td.studname { background: var(--sel); }
.grade-table tr.row-focus td.studname .rowname { font-weight: 800; }

.studname { text-align: left !important; }
.studname .rowname { display: inline-block; max-width: 130px; overflow: hidden; text-overflow: ellipsis; vertical-align: middle; cursor: pointer; padding: 6px 8px; font-size: 13px; }
.studname .rowbtn { font-size: 10px; color: #999; padding-left: 3px; cursor: pointer; }
.studname .rowbtn:hover { color: var(--ink); }

.rank-badge {
  display: inline-block; width: 20px; height: 20px; line-height: 20px;
  border: 1px solid var(--line); border-radius: 50%; font-size: 11px;
  margin-right: 6px; text-align: center; font-family: var(--mono); background:#fff;
}
.rank-top { background: var(--ink); color: #fff; border-color: var(--ink); }

/* stats strip under header */
.stats-strip td {
  background: #fbfbfb; font-size: 11px; color: var(--ink-soft);
  border-bottom: 2px solid var(--line); font-family: var(--mono); padding: 4px 8px;
}
.stats-strip td strong { color: var(--ink); font-size: 12px; }

/* ---------- Ranking panel ---------- */
.rank-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 4px 14px; }
.rank-item { display: flex; align-items: center; gap: 8px; font-size: 13px; padding: 2px 0; border-bottom: 1px dotted var(--line-soft); }
.rank-item .rn { width: 26px; font-family: var(--mono); font-weight: 800; }
.rank-item .rname { flex: 1; overflow: hidden; text-overflow: ellipsis; }
.rank-item .ravg { font-family: var(--mono); font-weight: 700; }
.rank-item .bar { width: 46px; height: 8px; }

/* shortlist of missing students */
.missing-tags { display: flex; flex-wrap: wrap; gap: 4px; }
.tag {
  font-size: 11px; border: 1px solid var(--line-soft); border-radius: 2px;
  padding: 1px 6px; background: #fff;
}
.tag-bad { border-color: var(--bad); color: var(--bad); background: var(--bad-bg); }

/* ---------- Modal ---------- */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 100;
  display: flex; align-items: flex-start; justify-content: center;
  padding: 8vh 16px 20px; overflow: auto;
}
.modal {
  background: #fff; border: 2px solid var(--line); width: 100%; max-width: 420px;
  border-radius: 2px; box-shadow: 8px 8px 0 rgba(0,0,0,.08);
}
.modal-wide { max-width: 640px; }
.modal-head {
  padding: 12px 16px; font-weight: 800; font-size: 15px;
  border-bottom: 1px solid var(--line-soft);
  display: flex; align-items: center; justify-content: space-between;
}
.modal-body { padding: 14px 16px; }
.modal-body .modal-text { margin: 6px 0; }
.modal-foot {
  padding: 10px 16px; border-top: 1px solid var(--line-soft);
  display: flex; justify-content: flex-end; gap: 8px; flex-wrap: wrap;
}
.field-label { display: block; font-size: 12px; font-weight: 700; margin: 4px 0 4px; color: var(--ink-soft); }
.input, textarea.input {
  border: 1px solid var(--line); background: #fff; padding: 7px 9px;
  font-size: 13px; border-radius: 2px; outline: none; font-family: inherit;
  color: var(--ink); width: 100%;
}
.input:focus { box-shadow: 0 0 0 2px rgba(0,0,0,.15); }
textarea.input { resize: vertical; min-height: 90px; font-family: var(--mono); }
.w-full { width: 100%; }
.field { margin-bottom: 10px; }
.field-row { display: flex; gap: 8px; }
.field-row > * { flex: 1; }
.hint { font-size: 11px; color: var(--ink-soft); margin-top: 4px; }
.kbd { border: 1px solid var(--line-soft); border-bottom-width: 2px; border-radius: 2px; padding: 0 4px; font-family: var(--mono); font-size: 11px; background:#fff; }

/* ---------- Menu ---------- */
.menu-overlay { position: fixed; inset: 0; z-index: 90; }
.menu {
  position: fixed; background: #fff; border: 1px solid var(--line);
  box-shadow: 4px 4px 0 rgba(0,0,0,.08); border-radius: 2px; padding: 4px; opacity: 0; transition: opacity .08s; z-index: 91;
}
.menu-item {
  display: flex; align-items: center; gap: 8px; width: 100%;
  background: none; border: none; text-align: left; padding: 7px 9px;
  font-size: 13px; cursor: pointer; border-radius: 2px; font-family: inherit; color: var(--ink);
}
.menu-item:hover { background: var(--sel); }
.menu-item-danger { color: var(--bad); }
.menu-item-danger:hover { background: var(--bad-bg); }
.menu-ico { width: 16px; text-align: center; }
.menu-sep { height: 1px; background: var(--line-soft); margin: 4px 6px; }

/* ---------- Toast ---------- */
.toast-root { position: fixed; bottom: 18px; right: 18px; z-index: 200; display: flex; flex-direction: column; gap: 8px; }
.toast {
  background: var(--ink); color: #fff; padding: 10px 16px; border-radius: 2px;
  font-size: 13px; box-shadow: 3px 3px 0 rgba(0,0,0,.18); opacity: 0; transform: translateY(6px);
  transition: opacity .25s, transform .25s; max-width: 320px;
}
.toast.show { opacity: 1; transform: none; }
.toast-warn { background: var(--warn); }
.toast-error { background: var(--bad); }
.toast-ok { background: var(--good); }

/* ---------- Footer ---------- */
.foot {
  margin-top: auto; border-top: 1px solid var(--line-soft);
  padding: 10px 16px; font-size: 11px; color: var(--ink-soft);
  display: flex; justify-content: space-between; flex-wrap: wrap; gap: 6px; background:#fff;
}

/* ---------- Print ---------- */
@media print {
  .topbar, .crumb, .toolbar, .foot, .panel .btn, .no-print, .table-scroller { max-height: none !important; }
  body, .app, .view { background: #fff; }
  .no-print { display: none !important; }
  .table-scroller { overflow: visible; border: none; }
  .grade-table th { position: static; }
  .grade-table td.studname, .grade-table th.studname { position: static; }
  .grade-table th.avg-col { background:#fff; }
}