:root {
  --ink: #1d1a16;
  --muted: #5e584e;
  --navy: #16324f;
  --navy-deep: #0e2438;
  --paper: #f3efe6;
  --card: #fffdf8;
  --line: #e3d8c6;
  --red: #8e2b2b;
  --red-bg: #fdeceb;
  --green: #1c6b43;
  --green-bg: #e6f5ed;
  --amber: #8a5a12;
  --amber-bg: #fff4df;
  --focus: #c9852a;
  --shadow: 0 12px 32px rgba(22, 50, 79, 0.08);
  --sans: "PingFang TC", "Microsoft JhengHei", "Noto Sans TC", "Heiti TC", sans-serif;
  --serif: "Songti TC", "PMingLiU", "MingLiU", "Noto Serif TC", serif;
}

* { box-sizing: border-box; }
html { touch-action: manipulation; }
body {
  margin: 0;
  color: var(--ink);
  background: var(--paper);
  font-family: var(--sans);
  font-size: 16px;
  line-height: 1.5;
  overflow-x: hidden;
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
[hidden] { display: none !important; }
.booting { padding: 48px 20px; text-align: center; color: var(--muted); }

.page { min-height: 100vh; min-height: 100dvh; }
.top {
  background: var(--navy);
  color: #f7f3ea;
  padding: calc(16px + env(safe-area-inset-top)) 16px 14px;
  border-bottom: 4px solid #9d2c2c;
  box-shadow: inset 0 -4px 0 #f2d48a;
}
.top-row, .whoami, .brand, .datebar, .toolbar, .savebar-actions, .filters, .person, .pupil, .split {
  display: flex;
  align-items: center;
  gap: 10px;
}
.top-row { justify-content: space-between; align-items: flex-start; }
.brand { gap: 12px; min-width: 0; }
.brand > div { min-width: 0; }
.crest {
  width: 54px;
  height: 64px;
  flex: 0 0 auto;
  object-fit: contain;
}
.gate-head .crest { width: 96px; height: 114px; margin: 0 auto 8px; }
.school { margin: 0; font-size: 13px; letter-spacing: 0.04em; opacity: 0.86; }
.top h1 { margin: 0; font-family: var(--serif); font-size: 1.35rem; font-weight: 700; }
.whoami { flex-wrap: wrap; justify-content: flex-end; }
.role-pill {
  background: rgba(255, 253, 248, 0.12);
  border: 1px solid rgba(242, 212, 138, 0.45);
  border-radius: 999px;
  padding: 4px 10px;
  font-size: 14px;
}
.nav {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 10px 12px 12px;
  background: var(--navy-deep);
}
.nav button, .ghost, .mark, .primary, .danger, .tiny {
  min-height: 44px;
  border-radius: 10px;
  border: 1px solid transparent;
  padding: 8px 14px;
}
.nav button {
  background: transparent;
  color: #f7f3ea;
  border-color: transparent;
  white-space: nowrap;
}
.nav button[aria-current="page"] {
  background: var(--paper);
  color: var(--navy);
  font-weight: 700;
}
.main { padding: 16px 14px 40px; max-width: 1120px; margin: 0 auto; min-width: 0; }
.sheet, .card, .subject, .day, .stack { min-width: 0; }
.sheet, .card, .subject, .day {
  background: var(--card);
  border: 1px solid var(--line);
  border-left: 6px solid #9d2c2c;
  border-radius: 14px;
  box-shadow: var(--shadow);
}
.sheet, .card { padding: 16px; }
.password-panel { margin-bottom: 16px; }
.stack { display: grid; gap: 14px; }
.banner { border-radius: 10px; padding: 10px 12px; margin-bottom: 12px; }
.banner:not([hidden]) { position: sticky; top: 8px; z-index: 30; }
.deadline { margin: 4px 0 0; font-weight: 800; color: var(--navy); }
.person.just-added { outline: 3px solid var(--navy); border-radius: 10px; }
.subject.tone-chinese { background: #ffe3ee; }
.subject.tone-english { background: #fff3bf; }
.subject.tone-maths { background: #dff5d8; }
.subject.tone-general { background: #d9ecff; }
.subject.tone-human { background: #f0e4ff; }
.subject.tone-science { background: #d8f3ee; }
.subject.tone-other { background: #f8ead7; }
.subject.tone-note { background: #f1eee8; }
.banner.good { background: var(--green-bg); color: var(--green); }
.banner.bad { background: var(--red-bg); color: var(--red); }
h2 { margin: 0 0 8px; font-size: 1.25rem; }
h3 { margin: 0; font-size: 1.05rem; }
.muted { color: var(--muted); }
.okline { color: var(--green); font-weight: 700; margin: 8px 0; }
.teacher-line { color: var(--navy); font-weight: 700; }
label { display: grid; gap: 6px; font-weight: 700; }
input, select, textarea {
  width: 100%;
  min-height: 44px;
  padding: 8px 10px;
  border-radius: 10px;
  border: 1px solid #cbbfaa;
  background: #fff;
  font-size: 16px;
}
input[type="checkbox"] { width: 22px; min-height: 22px; padding: 0; }
textarea { min-height: 140px; resize: vertical; }
.form-grid, .lookup-grid { display: grid; gap: 12px; }
.primary { background: var(--navy); color: #fffdf8; font-weight: 700; }
.ghost { background: #fff; border-color: #cbbfaa; }
.danger { background: var(--red-bg); color: var(--red); border-color: #f3c7c3; }
.tiny { min-height: 36px; padding: 4px 10px; background: #fff; border-color: #cbbfaa; }
.top .ghost { background: transparent; color: #fffdf8; border-color: rgba(255, 253, 248, 0.35); }

.gate { max-width: 460px; margin: 28px auto; padding: 0 14px 32px; display: grid; gap: 14px; }
.gate-head { text-align: center; padding: 12px 0 4px; }
a.ghost { display: inline-flex; align-items: center; justify-content: center; text-decoration: none; color: inherit; padding: 8px 12px; }
.gate-head h1 { margin: 0; font-family: var(--serif); color: var(--navy); font-size: 2rem; }
.gate-head p { margin: 4px 0 0; color: var(--muted); }
.due-picker { display: grid; gap: 8px; }
.due-current { justify-content: flex-start; background: #fff; }
.calendar { border: 1px solid #cbbfaa; border-radius: 12px; padding: 10px; background: #fff; display: grid; gap: 8px; }
.cal-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.cal-head button { min-height: 36px; }
.cal-week, .cal-grid { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 4px; text-align: center; }
.cal-week { color: var(--muted); font-size: 13px; font-weight: 700; }
.cal-day { min-height: 36px; padding: 4px; background: #fff; }
.cal-day.is-holiday { background: #f6c1c1; color: #8d1d18; font-weight: 800; }
.cal-day.is-selected { outline: 2px solid var(--navy); }

.grade-block { display: grid; gap: 8px; }
.class-grid, .filters, .segment {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.class-grid { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); }
.class-grid button, .segment button {
  min-height: 52px;
  border-radius: 12px;
  border: 1px solid #cbbfaa;
  background: #fff;
  font-weight: 800;
  font-size: 1.05rem;
}
.class-grid button { display: grid; gap: 2px; align-content: center; justify-items: center; padding: 8px 4px; }
.class-homeroom { font-size: 12px; font-weight: 600; line-height: 1.3; color: inherit; opacity: 0.78; overflow-wrap: anywhere; text-align: center; }
.homeroom-line { margin: 2px 0 0; color: var(--navy); font-weight: 800; }
.segment button[aria-pressed="true"],
.filters button[aria-pressed="true"],
.class-grid button:active {
  background: var(--navy);
  color: #fff;
  border-color: var(--navy);
}
.datebar, .toolbar { flex-wrap: wrap; }
.datebar input { max-width: 220px; }
.subject { display: flex; flex-direction: column; padding: 12px; }
.subject-head { display: flex; justify-content: space-between; gap: 8px; flex-wrap: wrap; align-items: baseline; }
.counts { font-variant-numeric: tabular-nums; }
.miss { color: var(--red); font-weight: 800; }
.done { color: var(--green); font-weight: 700; }
.none {
  margin: 8px 0 0;
  padding: 8px 10px;
  border-radius: 8px;
  background: #f6f1e7;
  color: var(--muted);
}
.homework-text {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  margin: 8px 0 0;
  font-size: 1.05rem;
  line-height: 1.7;
}
.hw-item {
  display: flex;
  gap: 10px;
  align-items: baseline;
  padding-top: 10px;
  border-top: 1px solid var(--line);
  margin-top: 10px;
}
.hw-no {
  min-width: 1.8em;
  font-weight: 800;
  color: var(--navy);
}
.hw-line {
  display: flex;
  flex: 1;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 4px 16px;
  min-width: 0;
}
.hw-item .homework-text { margin: 0; flex: 1 1 12em; }
.hw-remind {
  background: #fff1e4;
  border-radius: 8px;
  margin-left: -6px;
  margin-right: -6px;
  padding-left: 6px;
  padding-right: 6px;
}
.hw-remind .homework-text,
.hw-remind .due { color: #8a3d12; }
.remind-tag {
  color: #8a3d12;
  font-weight: 800;
  white-space: nowrap;
}
.homework-text a { color: var(--navy); font-weight: 800; text-decoration: underline; }
.content-row { display: flex; align-items: flex-end; gap: 8px; }
.preset-field { flex: 0 0 6.5em; width: 6.5em; }
.preset-field select { text-overflow: ellipsis; }
.content-field { flex: 1 1 auto; min-width: 0; }
.content-label, .link-add { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.content-label { justify-content: space-between; font-weight: 700; }
.link-add label { flex: 1 1 160px; }
.draft-link { margin: 0; }
.draft-link .tiny { margin-left: 8px; vertical-align: middle; }
.editor-line { color: var(--muted); white-space: nowrap; }
.due { color: var(--navy); font-weight: 700; margin: 0; white-space: nowrap; }
.card-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
  margin-top: auto;
  padding-top: 12px;
}
.grade-tick h3 { margin: 0; }
.checkline {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
}
.check-table { width: 100%; border-collapse: collapse; }
.check-table th, .check-table td {
  border-bottom: 1px solid var(--line);
  padding: 8px 6px;
  text-align: center;
}
.check-table th:first-child, .check-table td:first-child { text-align: left; }
.mark-yes { color: var(--green); font-weight: 800; }
.mark-no { color: var(--red); font-weight: 800; }
.grid-pups {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(148px, 1fr));
  gap: 8px;
  margin-top: 10px;
}
.chip {
  display: flex;
  gap: 6px;
  align-items: baseline;
  border-radius: 10px;
  padding: 8px;
  min-height: 44px;
  background: var(--green-bg);
}
.chip.out { background: var(--red-bg); }
.chip b { min-width: 1.4em; }
.chip span { margin-left: auto; font-size: 13px; font-weight: 800; }
.chip.in span { color: var(--green); }
.chip.out span { color: var(--red); }

.pupil-list { display: grid; gap: 8px; margin: 12px 0 8px; padding-bottom: 96px; }
.pupil {
  justify-content: space-between;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 8px 8px 8px 12px;
  background: #fff;
}
.pupil.is-out { border-color: #f0c2be; background: #fff8f7; }
.pupil.is-in { border-color: #cfe5d8; }
.no {
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  border-radius: 10px;
  background: var(--navy);
  color: #fff;
  font-weight: 800;
}
.who { flex: 1; font-weight: 700; }
.mark { min-width: 84px; font-weight: 800; background: var(--red-bg); color: var(--red); }
.mark[aria-pressed="true"] { background: var(--green-bg); color: var(--green); }
.savebar {
  position: sticky;
  bottom: 0;
  display: grid;
  gap: 8px;
  padding: 10px 0 calc(10px + env(safe-area-inset-bottom));
  background: linear-gradient(transparent, var(--paper) 18%);
}
.savebar-actions { flex-wrap: wrap; }
.savebar .primary { flex: 1; }

.person {
  justify-content: space-between;
  flex-wrap: wrap;
  padding: 8px 0;
  border-bottom: 1px solid var(--line);
  align-items: flex-start;
}
.person > form { flex: 1 0 100%; }
.person.off { opacity: 0.62; }
.person-actions { display: flex; flex-wrap: wrap; gap: 6px; justify-content: flex-end; }
.assign-table { display: grid; gap: 10px; }
.assign-row {
  display: grid;
  grid-template-columns: 52px 120px 120px repeat(var(--cols, 5), minmax(110px, 1fr));
  gap: 8px;
  align-items: end;
}
.assign-name { display: flex; align-items: center; justify-content: space-between; gap: 4px; font-size: 14px; }
.assign-row strong { padding-bottom: 10px; }
.assign-caption { display: none; font-size: 13px; font-weight: 700; color: var(--muted); }
.assign-cell { display: grid; gap: 4px; }
.table-scroll { overflow-x: auto; max-width: 100%; -webkit-overflow-scrolling: touch; }
.check-table { width: 100%; }
.check-table th, .check-table td { white-space: nowrap; }
.check-table th:first-child, .check-table td:first-child {
  position: sticky;
  left: 0;
  background: var(--card);
  z-index: 1;
}
details { margin-top: 8px; }
summary { cursor: pointer; min-height: 44px; display: flex; align-items: center; font-weight: 700; }
.day + .day { margin-top: 12px; }
.day { padding: 12px; }
.inline-missing { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }

@media (min-width: 700px) {
  .form-grid.two, .lookup-grid { grid-template-columns: 1fr 1fr; }
  .datebar { width: auto; }
  .datebar input { flex: 0 1 200px; }
  .datebar button { flex: 0 0 auto; }
}
@media (min-width: 900px) {
  .top { padding-left: max(28px, env(safe-area-inset-left)); padding-right: max(28px, env(safe-area-inset-right)); }
  .top h1 { font-size: 1.7rem; }
  .main { padding: 22px 28px 48px; }
  .split { align-items: flex-start; }
  .split > * { flex: 1; }
  .class-grid { grid-template-columns: repeat(5, minmax(0, 1fr)); }
}
@media (max-width: 899px) {
  .class-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .hw-line { flex-direction: column; align-items: flex-start; gap: 2px; }
  .hw-item .homework-text { flex: 0 1 auto; width: 100%; }
  .editor-line, .due, .remind-tag { white-space: normal; }
  .assign-row { grid-template-columns: 1fr; }
  .assign-row:first-child { display: none; }
  .assign-caption { display: block; }
}
@media (max-width: 640px) {
  .class-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .top-row { flex-direction: column; gap: 12px; }
  .whoami { justify-content: space-between; width: 100%; }
  .datebar { display: grid; grid-template-columns: 1fr 1fr; width: 100%; }
  .datebar input { grid-column: 1 / -1; max-width: none; order: -1; }
  .card-actions button { flex: 1 1 calc(50% - 8px); }
  .segment button { flex: 1 1 calc(50% - 8px); }
  .person-actions { width: 100%; justify-content: flex-start; }
  .gate { margin-top: 12px; }
  .gate-head h1 { font-size: 1.7rem; }
  .main { padding-left: 12px; padding-right: 12px; }
  .sheet, .card { padding: 12px; }
  .cal-day { min-height: 42px; }
}
@media (max-width: 380px) {
  .class-grid { grid-template-columns: 1fr; }
}
@media (pointer: coarse) {
  .tiny, .cal-day, .cal-head button { min-height: 44px; }
}
