:root {
  --bg: #f5f6f8;
  --bg-elev: #ffffff;
  --bg-elev-2: #fafbfc;
  --border: #e2e5ea;
  --text: #1a1d23;
  --text-muted: #6b7280;
  --text-faint: #9aa1ac;
  --accent: #3457d5;
  --accent-soft: #e8edfc;
  --accent-text: #2743ac;
  --good: #147a4c;
  --good-soft: #e3f6ec;
  --warn: #b45309;
  --warn-soft: #fdf1de;
  --shadow: 0 1px 2px rgba(20, 24, 33, 0.04), 0 8px 24px rgba(20, 24, 33, 0.06);
  --radius: 14px;
  --radius-sm: 9px;
  --mono: "SF Mono", ui-monospace, "Cascadia Code", Menlo, Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
}

:root[data-theme="dark"] {
  --bg: #14161a;
  --bg-elev: #1c1f26;
  --bg-elev-2: #22262e;
  --border: #2e333d;
  --text: #eceef1;
  --text-muted: #9aa1ac;
  --text-faint: #6b7280;
  --accent: #6d8bff;
  --accent-soft: #232a48;
  --accent-text: #a9bbff;
  --good: #4ade9b;
  --good-soft: #16302a;
  --warn: #f5b664;
  --warn-soft: #3a2c15;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.2), 0 8px 24px rgba(0, 0, 0, 0.35);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14161a;
    --bg-elev: #1c1f26;
    --bg-elev-2: #22262e;
    --border: #2e333d;
    --text: #eceef1;
    --text-muted: #9aa1ac;
    --text-faint: #6b7280;
    --accent: #6d8bff;
    --accent-soft: #232a48;
    --accent-text: #a9bbff;
    --good: #4ade9b;
    --good-soft: #16302a;
    --warn: #f5b664;
    --warn-soft: #3a2c15;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.2), 0 8px 24px rgba(0, 0, 0, 0.35);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  -webkit-font-smoothing: antialiased;
}

body {
  min-height: 100vh;
}

.wrap {
  max-width: 1180px;
  margin: 0 auto;
  padding: 28px 20px 80px;
}

/* ---------- header ---------- */
.hero {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 22px;
}

.hero h1 {
  font-size: 26px;
  font-weight: 700;
  margin: 0 0 6px;
  letter-spacing: -0.01em;
}

.hero p {
  margin: 0;
  color: var(--text-muted);
  font-size: 14.5px;
  max-width: 640px;
  line-height: 1.55;
}

.hero-actions {
  display: flex;
  gap: 8px;
  flex-shrink: 0;
}

.icon-btn {
  border: 1px solid var(--border);
  background: var(--bg-elev);
  color: var(--text);
  width: 38px;
  height: 38px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: 16px;
  transition: background 0.15s, border-color 0.15s;
}
.icon-btn:hover { background: var(--bg-elev-2); border-color: var(--accent); }

/* ---------- stat chips ---------- */
.stat-row {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 20px;
}
.stat-chip {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 14px;
  min-width: 100px;
}
.stat-chip .num {
  font-size: 19px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.stat-chip .label {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 1px;
}

/* ---------- controls ---------- */
.panel {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  margin-bottom: 18px;
}

.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: flex-end;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.field label {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
}

.field input[type="text"],
.field select {
  font: inherit;
  font-size: 14px;
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 9px;
  padding: 9px 11px;
  min-width: 200px;
  outline: none;
  transition: border-color 0.15s;
}
.field input[type="text"]:focus,
.field select:focus { border-color: var(--accent); }

.segmented {
  display: inline-flex;
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 3px;
  gap: 3px;
}
.segmented button {
  border: none;
  background: transparent;
  color: var(--text-muted);
  font: inherit;
  font-size: 13.5px;
  font-weight: 600;
  padding: 7px 13px;
  border-radius: 7px;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s, color 0.15s;
}
.segmented button.active {
  background: var(--accent);
  color: #fff;
}
.segmented button:not(.active):hover { color: var(--text); }

.btn-reset {
  border: 1px solid var(--border);
  background: var(--bg-elev-2);
  color: var(--text-muted);
  font: inherit;
  font-size: 13.5px;
  font-weight: 600;
  padding: 9px 13px;
  border-radius: 9px;
  cursor: pointer;
  height: 38px;
}
.btn-reset:hover { color: var(--text); border-color: var(--accent); }

.method-note {
  font-size: 12.5px;
  color: var(--text-muted);
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px dashed var(--border);
  line-height: 1.5;
}
.method-note strong { color: var(--text); }
.method-note a.link-btn {
  color: var(--accent-text);
  cursor: pointer;
  font-weight: 600;
  text-decoration: none;
}
.method-note a.link-btn:hover { text-decoration: underline; }

/* ---------- result meta ---------- */
.result-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
  font-size: 13px;
  color: var(--text-muted);
}

/* ---------- table ---------- */
.table-scroll {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

table {
  width: 100%;
  min-width: 640px;
  border-collapse: collapse;
  font-size: 13.5px;
}

thead th {
  position: sticky;
  top: 0;
  background: var(--bg-elev-2);
  text-align: left;
  padding: 11px 12px;
  font-size: 12px;
  font-weight: 700;
  color: var(--text-muted);
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
  z-index: 1;
}
thead th:hover { color: var(--text); }
thead th.active { color: var(--accent-text); }
thead th .arrow { margin-left: 3px; opacity: 0.7; font-size: 10px; }
thead th.num, td.num { text-align: right; }

tbody tr {
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: background 0.1s;
}
tbody tr:last-child { border-bottom: none; }
tbody tr:hover { background: var(--bg-elev-2); }
tbody tr.expanded { background: var(--accent-soft); }

td {
  padding: 10px 12px;
  vertical-align: top;
}

.rank-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 26px;
  height: 22px;
  padding: 0 6px;
  border-radius: 7px;
  font-weight: 700;
  font-size: 12.5px;
  font-variant-numeric: tabular-nums;
  background: var(--bg-elev-2);
  color: var(--text-muted);
}
.rank-badge.top { background: var(--good-soft); color: var(--good); }

.school-name { color: var(--text-muted); font-size: 12px; margin-bottom: 1px; }
.dept-name { font-weight: 600; }

.formula-chips { display: flex; flex-wrap: wrap; gap: 4px; }
.chip {
  font-family: var(--mono);
  font-size: 11px;
  padding: 2px 6px;
  border-radius: 6px;
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  color: var(--text-muted);
  white-space: nowrap;
}
.chip.unscorable { background: var(--warn-soft); color: var(--warn); border-color: transparent; }

.score-primary {
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  font-size: 14px;
}
.score-secondary {
  color: var(--text-faint);
  font-size: 11.5px;
  font-variant-numeric: tabular-nums;
}
.na { color: var(--text-faint); font-style: normal; }

.detail-row td {
  background: var(--bg-elev-2);
  padding: 14px 20px 18px;
}
.detail-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 10px;
}
.detail-card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 10px 12px;
}
.detail-card .k { font-size: 11px; color: var(--text-muted); margin-bottom: 3px; }
.detail-card .v { font-size: 14px; font-weight: 600; font-variant-numeric: tabular-nums; }
.detail-formula-table { width: 100%; border-collapse: collapse; margin-top: 4px; font-size: 12.5px; }
.detail-formula-table th, .detail-formula-table td {
  text-align: right; padding: 4px 8px; border-bottom: 1px solid var(--border);
}
.detail-formula-table th:first-child, .detail-formula-table td:first-child { text-align: left; }
.detail-formula-table thead th { position: static; font-size: 11px; }

/* ---------- load more ---------- */
.load-more-row td {
  text-align: center;
  padding: 16px;
}
.btn-load-more {
  border: 1px solid var(--border);
  background: var(--bg-elev-2);
  color: var(--accent-text);
  font: inherit;
  font-weight: 600;
  font-size: 13px;
  padding: 9px 18px;
  border-radius: 9px;
  cursor: pointer;
}
.btn-load-more:hover { border-color: var(--accent); }

.empty-state {
  padding: 60px 20px;
  text-align: center;
  color: var(--text-muted);
}

/* ---------- modal ---------- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(10, 12, 16, 0.5);
  display: none;
  align-items: flex-start;
  justify-content: center;
  padding: 40px 16px;
  z-index: 50;
  overflow-y: auto;
}
.modal-backdrop.open { display: flex; }
.modal {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  max-width: 680px;
  width: 100%;
  padding: 26px 28px 28px;
}
.modal h2 { margin: 0 0 14px; font-size: 19px; }
.modal h3 { font-size: 14.5px; margin: 20px 0 8px; }
.modal p, .modal li { font-size: 13.5px; line-height: 1.65; color: var(--text-muted); }
.modal code {
  font-family: var(--mono);
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  padding: 1px 5px;
  border-radius: 5px;
  font-size: 12.5px;
  color: var(--text);
}
.modal .formula-block {
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 12px 14px;
  font-family: var(--mono);
  font-size: 13px;
  margin: 8px 0 14px;
  color: var(--text);
  overflow-x: auto;
  white-space: pre;
}
.modal-close-row { display: flex; justify-content: flex-end; margin-top: 18px; }
.modal .subj-table { width: 100%; border-collapse: collapse; font-size: 12.5px; margin-top: 6px; }
.modal .subj-table th, .modal .subj-table td { text-align: right; padding: 5px 8px; border-bottom: 1px solid var(--border); }
.modal .subj-table th:first-child, .modal .subj-table td:first-child { text-align: left; color: var(--text); }
.modal .subj-table thead th { color: var(--text-muted); font-weight: 600; }

footer {
  margin-top: 28px;
  font-size: 12px;
  color: var(--text-faint);
  line-height: 1.7;
}
footer a { color: var(--text-faint); }

@media (max-width: 640px) {
  .field input[type="text"], .field select { min-width: 140px; }
  thead th, td { padding: 8px; }
}
