:root {
  --bg: #f5f6fa;
  --card: #ffffff;
  --line: #e6e8f0;
  --text: #1c2233;
  --text-2: #6b7386;
  --text-3: #9aa1b1;
  --brand: #4f6bed;
  --brand-soft: #eef1fe;
  --up: #e04a4a;
  --down: #12a05f;
  --good: #12a05f;
  --warn: #e8912d;
  --radius: 14px;
  --shadow: 0 1px 3px rgba(24, 32, 60, .06), 0 8px 24px rgba(24, 32, 60, .05);
  --nav-h: 58px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0; padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Helvetica Neue", "Microsoft YaHei", sans-serif;
  font-size: 15px;
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
}

button, input, select, textarea { font-family: inherit; font-size: inherit; color: inherit; }
button { cursor: pointer; border: none; background: none; }
a { color: inherit; text-decoration: none; }

/* ============ 登录 ============ */
.login-mask {
  position: fixed; inset: 0; z-index: 900;
  background: linear-gradient(160deg, #4f6bed 0%, #6d5ce7 55%, #8a5be0 100%);
  display: flex; align-items: center; justify-content: center;
  padding: 24px;
}
.login-mask.hidden { display: none; }
.login-card {
  width: 100%; max-width: 360px;
  background: #fff; border-radius: 20px;
  padding: 34px 26px 28px;
  box-shadow: 0 20px 60px rgba(20, 25, 60, .28);
  text-align: center;
}
.login-logo { margin-bottom: 12px; }
.login-title { margin: 0 0 6px; font-size: 22px; font-weight: 700; letter-spacing: .5px; }
.login-sub { margin: 0 0 24px; font-size: 13px; color: var(--text-3); }
.login-input {
  width: 100%; height: 46px; margin-bottom: 12px;
  padding: 0 14px; border: 1px solid var(--line); border-radius: 10px;
  background: #fafbfd; outline: none; transition: .15s;
}
.login-input:focus { border-color: var(--brand); background: #fff; box-shadow: 0 0 0 3px var(--brand-soft); }
.login-btn {
  width: 100%; height: 46px; margin-top: 6px;
  background: var(--brand); color: #fff;
  border-radius: 10px; font-size: 16px; font-weight: 600;
  transition: .15s;
}
.login-btn:active { transform: scale(.98); background: #4159d8; }
.login-btn:disabled { opacity: .55; }
.login-msg { min-height: 20px; margin: 12px 0 0; font-size: 13px; color: var(--up); }

/* ============ 顶栏 ============ */
.topbar {
  position: sticky; top: 0; z-index: 60;
  display: flex; align-items: center; justify-content: space-between;
  gap: 10px;
  /* 高度必须含安全区：standalone 模式下刘海屏顶部有 44px 左右的 inset，
     若 height 写死 54px 而 padding-top 另加，内容会被挤出可视区导致标题看不见 */
  min-height: 54px;
  height: calc(54px + env(safe-area-inset-top, 0px));
  padding: env(safe-area-inset-top, 0px) 14px 0;
  background: rgba(255, 255, 255, .92);
  backdrop-filter: saturate(180%) blur(12px);
  -webkit-backdrop-filter: saturate(180%) blur(12px);
  border-bottom: 1px solid var(--line);
}
.tb-left { display: flex; align-items: baseline; gap: 8px; min-width: 0; }
.tb-brand {
  font-size: 17px; font-weight: 700; white-space: nowrap;
  display: flex; align-items: center; gap: 6px;
}
.tb-brand::before {
  content: ''; width: 20px; height: 20px; border-radius: 6px; flex-shrink: 0;
  background: linear-gradient(135deg, #4f6bed, #6d5ce7);
}
.tb-title { font-size: 13px; font-weight: 500; color: var(--text-3); white-space: nowrap; }
.tb-sub { font-size: 12px; color: var(--text-3); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tb-right { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }

/* 顶栏水平导航：默认隐藏，桌面端显示 */
.topnav { display: none; gap: 2px; }
.topnav-item {
  padding: 6px 14px; border-radius: 8px;
  font-size: 14px; color: var(--text-2); white-space: nowrap;
  transition: .15s;
}
.topnav-item:hover { background: var(--bg); color: var(--text); }
.topnav-item.on { background: var(--brand-soft); color: var(--brand); font-weight: 600; }

.sync-badge {
  font-size: 11px; padding: 3px 8px; border-radius: 20px;
  background: #f0f2f7; color: var(--text-2); white-space: nowrap;
}
.sync-badge.ok { background: #e7f7ef; color: var(--good); }
.sync-badge.err { background: #fdecec; color: var(--up); }
.sync-badge.busy { background: var(--brand-soft); color: var(--brand); }

.user-menu-wrap { position: relative; }
.user-btn {
  width: 32px; height: 32px; border-radius: 50%;
  background: var(--brand); color: #fff;
  font-size: 13px; font-weight: 600;
  display: flex; align-items: center; justify-content: center;
}
.user-menu {
  position: absolute; right: 0; top: 40px; width: 190px;
  background: #fff; border-radius: 12px;
  box-shadow: var(--shadow); border: 1px solid var(--line);
  padding: 6px; display: none; z-index: 80;
}
.user-menu.open { display: block; }
.um-head { padding: 10px 12px 12px; border-bottom: 1px solid var(--line); margin-bottom: 6px; }
.um-name { font-size: 14px; font-weight: 600; }
.um-meta { font-size: 11px; color: var(--text-3); margin-top: 2px; }
.um-item {
  display: block; width: 100%; text-align: left;
  padding: 9px 12px; border-radius: 8px; font-size: 14px; color: var(--text);
}
.um-item:hover { background: var(--bg); }
.um-item.danger { color: var(--up); }

/* ============ 主体 ============ */
.app {
  max-width: 960px; margin: 0 auto;
  padding: 14px 14px calc(var(--nav-h) + var(--safe-b) + 20px);
}
.boot-loading { padding: 60px 0; text-align: center; color: var(--text-3); }

/* ============ 卡片 ============ */
.card {
  background: var(--card); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 16px; margin-bottom: 12px;
}
.card-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 10px; margin-bottom: 12px;
}
.card-title { font-size: 15px; font-weight: 650; display: flex; align-items: center; gap: 6px; }
.card-title .dot { width: 6px; height: 6px; border-radius: 50%; background: var(--brand); }
.card-sub { font-size: 12px; color: var(--text-3); }

/* ============ 统计块 ============ */
.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(104px, 1fr)); gap: 10px; }
.stat {
  background: var(--card); border-radius: 12px; padding: 12px 13px;
  box-shadow: var(--shadow);
}
.stat-label { font-size: 11px; color: var(--text-3); white-space: nowrap; }
.stat-value { font-size: 22px; font-weight: 700; margin-top: 3px; letter-spacing: -.5px; }
.stat-value small { font-size: 12px; font-weight: 500; color: var(--text-3); margin-left: 2px; }
.stat-delta { font-size: 11px; margin-top: 2px; font-weight: 600; }
.delta-up { color: var(--up); }
.delta-down { color: var(--down); }
.delta-flat { color: var(--text-3); }
/* 「较上次」下方的说明：标明对比了哪几门共同科目 */
.stat-note {
  font-size: 10.5px; color: var(--text-3); margin-top: 3px; line-height: 1.45;
  overflow: hidden; text-overflow: ellipsis;
}

/* ============ 表单 ============ */
.field { margin-bottom: 14px; }
.field-label { display: block; font-size: 12px; color: var(--text-2); margin-bottom: 6px; font-weight: 500; }
.input, .select {
  width: 100%; height: 42px; padding: 0 12px;
  border: 1px solid var(--line); border-radius: 9px;
  background: #fafbfd; outline: none; transition: .15s;
}
.input:focus, .select:focus { border-color: var(--brand); background: #fff; box-shadow: 0 0 0 3px var(--brand-soft); }
textarea.input { height: auto; min-height: 76px; padding: 10px 12px; resize: vertical; }
.form-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; }

.btn {
  height: 42px; padding: 0 18px; border-radius: 9px;
  background: var(--brand); color: #fff; font-weight: 600; font-size: 14px;
  transition: .15s;
}
.btn:active { transform: scale(.98); }
.btn:disabled { opacity: .5; }
.btn.ghost { background: var(--brand-soft); color: var(--brand); }
.btn.outline { background: #fff; color: var(--text-2); border: 1px solid var(--line); }
.btn.danger { background: #fdecec; color: var(--up); }
.btn.sm { height: 34px; padding: 0 12px; font-size: 13px; }
.btn.block { width: 100%; }
.btn-row { display: flex; gap: 10px; flex-wrap: wrap; }

/* ============ 表格 ============ */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; margin: 0 -4px; padding: 0 4px; }
table.tbl { width: 100%; border-collapse: collapse; font-size: 13px; white-space: nowrap; }
.tbl th {
  text-align: left; font-weight: 600; color: var(--text-2); font-size: 12px;
  padding: 9px 10px; border-bottom: 1px solid var(--line); background: #fafbfd;
  position: sticky; top: 0;
}
.tbl th:first-child, .tbl td:first-child { padding-left: 10px; }
.tbl td { padding: 10px; border-bottom: 1px solid #f1f2f6; }
.tbl tr:last-child td { border-bottom: none; }
.tbl .num { text-align: right; font-variant-numeric: tabular-nums; }
.tbl .sub-name { font-weight: 600; }
.sub-tag {
  display: inline-block; padding: 1px 6px; border-radius: 4px;
  font-size: 11px; background: var(--brand-soft); color: var(--brand); margin-left: 6px;
}

/* ============ 徽标/标签 ============ */
.chip {
  display: inline-block; padding: 3px 9px; border-radius: 20px;
  font-size: 12px; background: var(--brand-soft); color: var(--brand); white-space: nowrap;
}
.chip.gray { background: #f0f2f7; color: var(--text-2); }
.chip.green { background: #e7f7ef; color: var(--good); }
.chip.red { background: #fdecec; color: var(--up); }
.chip.warn { background: #fef4e6; color: var(--warn); }

.exam-type {
  display: inline-block; padding: 2px 8px; border-radius: 5px;
  font-size: 11px; font-weight: 600;
}
.type-周考 { background: #eef4ff; color: #3a6bd8; }
.type-月考 { background: #f0ecfe; color: #7048c9; }
.type-期中 { background: #e7f7ef; color: #12925a; }
.type-期末 { background: #fdecec; color: #d13b3b; }
.type-其他 { background: #f0f2f7; color: #6b7386; }

/* ============ 空态 ============ */
.empty { text-align: center; padding: 40px 20px; color: var(--text-3); }
.empty-ico { font-size: 34px; opacity: .35; margin-bottom: 8px; }
.empty-txt { font-size: 13px; margin-bottom: 14px; }

/* ============ 图表 ============ */
.chart-wrap { width: 100%; overflow: hidden; }
.chart-wrap svg { display: block; width: 100%; height: auto; }
.legend { display: flex; flex-wrap: wrap; gap: 12px; margin-top: 10px; font-size: 12px; color: var(--text-2); }
.legend-item { display: flex; align-items: center; gap: 5px; }
.legend-swatch { width: 14px; height: 3px; border-radius: 2px; }
.legend-swatch.dot-swatch { width: 9px; height: 9px; border-radius: 50%; }

.chart-axis { stroke: #e6e8f0; stroke-width: 1; }
.chart-grid { stroke: #f1f2f6; stroke-width: 1; }
.chart-lbl { font-size: 10px; fill: #9aa1b1; }
.chart-lbl-x { font-size: 10px; fill: #6b7386; }

/* ============ 趋势视图 ============ */
.trend-toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 12px; }
.seg { display: flex; background: #eef0f5; border-radius: 8px; padding: 3px; gap: 2px; }
.seg button {
  padding: 5px 12px; border-radius: 6px; font-size: 13px; color: var(--text-2); white-space: nowrap;
}
.seg button.on { background: #fff; color: var(--brand); font-weight: 600; box-shadow: 0 1px 2px rgba(0,0,0,.06); }
.subject-picker { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.subject-picker button {
  padding: 4px 11px; border-radius: 20px; font-size: 12.5px;
  background: #f0f2f7; color: var(--text-2); border: 1px solid transparent;
}
.subject-picker button.on { background: var(--brand-soft); color: var(--brand); border-color: #cdd6fb; font-weight: 600; }
/* 趋势页口径提示 */
.trend-hint {
  font-size: 11.5px; color: var(--text-3); line-height: 1.6;
  padding: 8px 11px; margin-bottom: 10px;
  background: #fafbfd; border-radius: 8px; border: 1px solid var(--line);
}

/* ============ 总分与总排名 ============ */
.total-box {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(110px, 1fr)); gap: 10px;
}
.total-item {
  background: #fafbfd; border: 1px solid var(--line); border-radius: 11px;
  padding: 11px 12px; text-align: center;
}
.total-label { font-size: 12px; color: var(--text-2); font-weight: 600; white-space: nowrap; }
.total-value {
  font-size: 21px; font-weight: 700; margin-top: 4px; letter-spacing: -.5px;
  color: var(--brand); font-variant-numeric: tabular-nums;
}
.total-desc { font-size: 10.5px; color: var(--text-3); margin-top: 3px; line-height: 1.4; }
.total-rank-in { height: 38px; text-align: center; font-weight: 600; margin-top: 4px; }
.total-rank-card .total-value { color: var(--text); }
.total-rank-card .total-item:first-child .total-value { color: var(--brand); }

/* 记录卡片上的总排名行 */
.rec-rank {
  display: inline-block; margin-top: 6px;
  padding: 3px 10px; border-radius: 20px;
  background: var(--brand-soft); color: var(--brand);
  font-size: 12px; font-weight: 600;
}

/* ============ 记录列表 ============ */
.rec-card {
  background: var(--card); border-radius: 12px; padding: 13px 15px;
  box-shadow: var(--shadow); margin-bottom: 10px;
}
.rec-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 9px; }
.rec-title { font-size: 14.5px; font-weight: 650; display: flex; align-items: center; gap: 8px; }
.rec-date { font-size: 12px; color: var(--text-3); }
.rec-total { font-size: 12px; color: var(--text-2); }
.rec-total b { font-size: 16px; color: var(--brand); font-variant-numeric: tabular-nums; }
.mini-table { width: 100%; border-collapse: collapse; font-size: 12.5px; }
.mini-table th { font-size: 11px; color: var(--text-3); font-weight: 500; text-align: right; padding: 4px 3px; }
.mini-table th:first-child { text-align: left; }
.mini-table td { padding: 5px 3px; text-align: right; border-top: 1px solid #f4f5f8; font-variant-numeric: tabular-nums; }
.mini-table td:first-child { text-align: left; color: var(--text-2); }

/* ============ 设置 ============ */
.set-row {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; padding: 13px 0; border-bottom: 1px solid #f1f2f6;
}
.set-row:last-child { border-bottom: none; }
.set-label { font-size: 14px; }
.set-desc { font-size: 11.5px; color: var(--text-3); margin-top: 2px; }
.set-item { flex-shrink: 0; }

.switch { width: 46px; height: 27px; border-radius: 20px; background: #d8dbe4; position: relative; transition: .2s; }
.switch.on { background: var(--brand); }
.switch::after {
  content: ''; position: absolute; top: 3px; left: 3px;
  width: 21px; height: 21px; border-radius: 50%; background: #fff;
  transition: .2s; box-shadow: 0 1px 3px rgba(0,0,0,.2);
}
.switch.on::after { left: 22px; }

.subject-editor { display: grid; grid-template-columns: 1fr 88px 76px auto; gap: 8px; align-items: center; margin-bottom: 8px; }
.subject-editor .input { height: 38px; }
.subject-editor .input.full-num { text-align: center; }
.icon-del {
  width: 30px; height: 30px; border-radius: 8px; color: var(--text-3);
  display: flex; align-items: center; justify-content: center; font-size: 16px;
}
.icon-del:hover { background: #fdecec; color: var(--up); }

/* ============ 底部导航 ============ */
.nav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 70;
  display: flex; background: rgba(255,255,255,.94);
  backdrop-filter: saturate(180%) blur(12px);
  -webkit-backdrop-filter: saturate(180%) blur(12px);
  border-top: 1px solid var(--line);
  padding-bottom: var(--safe-b);
  height: calc(var(--nav-h) + var(--safe-b));
}
.nav-item {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 2px; color: var(--text-3); font-size: 10.5px;
}
.nav-item .nav-ico { font-size: 17px; line-height: 1; }
.nav-item .nav-txt { white-space: nowrap; }
.nav-item.on { color: var(--brand); font-weight: 600; }

/* ============ 弹窗 ============ */
.modal-mask {
  position: fixed; inset: 0; z-index: 200;
  background: rgba(18, 24, 45, .45);
  display: flex; align-items: center; justify-content: center;
  padding: 16px; backdrop-filter: blur(2px);
  overflow-y: auto;
}
.modal {
  background: #fff; border-radius: 16px; width: 100%; max-width: 420px;
  max-height: 86vh; overflow-y: auto; padding: 20px;
  box-shadow: 0 20px 60px rgba(20, 25, 60, .3);
  /* 防止长文案横向溢出被裁切 */
  overflow-wrap: break-word; word-break: break-word;
}
.modal-title { font-size: 16px; font-weight: 700; margin: 0 0 4px; }
.modal-sub { font-size: 12.5px; color: var(--text-3); margin: 0 0 16px; }
.modal-actions { display: flex; gap: 10px; margin-top: 18px; }
.modal-actions .btn { flex: 1; min-width: 0; }

/* 弹窗头部：标题 + 右上角关闭按钮。
   .modal-head 用 sticky 固定在弹窗顶部，长表单滚动到中间时仍能随时关闭。 */
.modal-head {
  position: sticky; top: -20px; z-index: 3;
  display: flex; align-items: flex-start; justify-content: space-between; gap: 12px;
  margin: -20px -20px 12px; padding: 16px 20px 10px;
  background: #fff;
  border-bottom: 1px solid var(--line);
  border-radius: 16px 16px 0 0;
}
.modal-head-text { min-width: 0; }
.modal-head .modal-sub { margin-bottom: 0; }
.modal-close {
  flex-shrink: 0;
  width: 30px; height: 30px; border-radius: 50%;
  background: #f0f2f7; color: var(--text-2);
  font-size: 20px; line-height: 1;
  display: flex; align-items: center; justify-content: center;
  transition: .15s;
}
.modal-close:hover { background: #fdecec; color: var(--up); }
.modal-close:active { transform: scale(.92); }

/* 宽弹窗：编辑成绩时需要容纳整张成绩表 */
.modal-wide { max-width: 760px; }
.edit-grid {
  display: grid; grid-template-columns: 2fr 1fr 1.4fr; gap: 12px;
}
.edit-sec-title {
  font-size: 13px; font-weight: 650; color: var(--text);
  margin: 4px 0 10px; padding-left: 9px;
  border-left: 3px solid var(--brand);
}
.edit-hint { font-size: 11.5px; color: var(--text-3); margin-top: 12px; line-height: 1.6; }
.tbl-edit td { padding: 7px 6px; }
.tbl-edit th { padding: 8px 6px; }
.tbl-edit .input { min-width: 58px; }

@media (max-width: 640px) {
  .edit-grid { grid-template-columns: 1fr; gap: 0; }
}

/* 窄屏下弹窗按钮可换行，避免挤压 */
@media (max-width: 420px) {
  .modal-mask { padding: 12px; align-items: flex-start; }
  .modal { padding: 16px; border-radius: 14px; max-height: 92vh; }
  .modal-actions { flex-direction: column-reverse; }
  .modal-actions .btn { width: 100%; }
  .modal-head {
    top: -16px;
    margin: -16px -16px 12px; padding: 13px 16px 9px;
    border-radius: 14px 14px 0 0;
  }
}

/* ============ Toast ============ */
.toast-host {
  position: fixed; left: 0; right: 0; z-index: 300;
  bottom: calc(var(--nav-h) + var(--safe-b) + 16px);
  display: flex; flex-direction: column; align-items: center; gap: 8px;
  pointer-events: none;
}
.toast {
  background: rgba(28, 34, 51, .92); color: #fff;
  padding: 9px 16px; border-radius: 20px; font-size: 13px;
  max-width: 80%; text-align: center;
  animation: toastIn .2s ease;
}
.toast.err { background: rgba(200, 50, 50, .94); }
.toast.ok { background: rgba(16, 140, 90, .94); }
@keyframes toastIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

/* ============ 响应式 ============ */
@media (max-width: 640px) {
  .app { padding: 12px 11px calc(var(--nav-h) + var(--safe-b) + 18px); }
  .card { padding: 14px; }
  .stat-value { font-size: 20px; }
  .tb-sub { display: none; }
  /* 窄屏：统计块固定 2 列，避免 4 个挤成一行导致文字截断 */
  .stat-grid { grid-template-columns: repeat(2, 1fr); }
  .stat { padding: 11px 12px; }
  .stat-value { font-size: 19px; }
  /* 底部导航图标在上、文字在下 */
  .nav-item { gap: 3px; }
}
@media (min-width: 900px) {
  .nav { display: none; }
  .topnav { display: flex; }
  .app { padding-bottom: 30px; max-width: 1000px; }
  .topbar { padding: 0 22px; }
  .tb-left { flex-shrink: 0; }
  .topnav { flex: 1; justify-content: center; }
}

@media print {
  .topbar, .nav, .toast-host, .btn { display: none !important; }
  body { background: #fff; }
  .card { box-shadow: none; border: 1px solid #ddd; }
}
