:root {
  --paper: #F6F4EE;
  --paper-raised: #FFFFFF;
  --ink: #21302A;
  --ink-soft: #5B6B63;
  --line: #DCD6C6;
  --teal: #2B6E64;
  --teal-dark: #1D4E47;
  --amber: #C98A3B;
  --good: #3F8A5E;
  --bad: #B5533D;
  --radius: 10px;
  --mono: 'IBM Plex Mono', monospace;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: 'Inter', sans-serif;
  font-size: 15px;
  line-height: 1.5;
}

h1, h2, h3 {
  font-family: 'Fraunces', serif;
  font-weight: 600;
  color: var(--ink);
  margin: 0 0 6px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 10px 16px;
  padding: 16px 32px;
  border-bottom: 1px solid var(--line);
  background: var(--paper-raised);
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  text-decoration: none;
  color: var(--ink);
}

.brand-mark {
  width: 34px; height: 34px;
  border-radius: 8px;
  background: var(--teal);
  color: white;
  font-family: var(--mono);
  font-weight: 500;
  font-size: 13px;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}

.brand-text {
  font-family: 'Fraunces', serif;
  font-weight: 600;
  font-size: 17px;
}
.brand-sub {
  display: block;
  font-family: 'Inter', sans-serif;
  font-weight: 400;
  font-size: 11px;
  color: var(--ink-soft);
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

nav {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 18px;
}
nav a {
  color: var(--ink-soft);
  text-decoration: none;
  font-weight: 500;
  font-size: 14px;
  white-space: nowrap;
}
nav a:hover { color: var(--teal-dark); }
.quit-link { color: var(--bad) !important; }
.quit-link:hover { color: #8F3F2E !important; }

@media (max-width: 640px) {
  .topbar { padding: 14px 16px; }
  .page { padding: 16px; }
  .brand-sub { display: none; }
}

.page {
  max-width: 1100px;
  margin: 0 auto;
  padding: 32px;
}

/* Flash messages */
.flash-stack { margin-bottom: 20px; display: flex; flex-direction: column; gap: 8px; }
.flash {
  padding: 10px 14px;
  border-radius: var(--radius);
  font-size: 14px;
  border: 1px solid transparent;
}
.flash-success { background: #E9F3EC; color: var(--good); border-color: #C9E3D0; }
.flash-error { background: #FBEAE6; color: var(--bad); border-color: #F0CFC5; }

/* Steps (index page) */
.steps {
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.step {
  display: flex;
  gap: 18px;
  background: var(--paper-raised);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 20px 24px;
  opacity: 0.55;
}
.step.step-active, .step.step-done { opacity: 1; }
.step.step-active { border-color: var(--teal); box-shadow: 0 0 0 1px var(--teal); }
.step-num {
  font-family: var(--mono);
  font-size: 13px;
  color: var(--teal);
  font-weight: 500;
  min-width: 24px;
}
.step h3 { font-size: 16px; margin-bottom: 4px; }
.step p { margin: 0 0 10px; color: var(--ink-soft); font-size: 14px; }

.inline-form { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
input[type=text], input[type=password], input[type=number], input[type=file], select, textarea {
  font-family: inherit;
  font-size: 14px;
  padding: 7px 10px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--paper-raised);
  color: var(--ink);
}
input[type=file] {
  width: 100%;
  max-width: 260%;
  min-width: 0;
}
.input-dirty {
  border-color: var(--amber) !important;
  background: #FCF6E8 !important;
}
button {
  font-family: inherit;
  font-size: 14px;
  font-weight: 600;
  padding: 7px 16px;
  border-radius: 7px;
  border: 1px solid var(--teal);
  background: var(--teal);
  color: white;
  cursor: pointer;
}
button:hover { background: var(--teal-dark); }
button:disabled { opacity: 0.4; cursor: not-allowed; }
.btn-primary { padding: 9px 20px; }
.btn-secondary {
  background: var(--paper-raised);
  color: var(--teal-dark);
  border: 1px solid var(--line);
  font-weight: 600;
}
.btn-secondary:hover { background: #EFEAD9; border-color: var(--teal); }
.btn-text-danger {
  background: none; border: none; color: var(--bad);
  font-weight: 500; font-size: 12px; padding: 0; text-decoration: underline;
}
.btn-danger {
  background: var(--bad);
  border-color: var(--bad);
}
.btn-danger:hover { background: #8F3F2E; }

.hint { color: var(--ink-soft); font-size: 13px; margin: 4px 0 0; }
.hint-good { color: var(--good); font-weight: 600; }

.class-grid { display: flex; flex-wrap: wrap; gap: 8px; }
.class-chip {
  padding: 8px 14px;
  border: 1px solid var(--line);
  border-radius: 20px;
  background: var(--paper-raised);
  text-decoration: none;
  color: var(--ink);
  font-weight: 500;
  font-size: 14px;
}
.class-chip:hover { border-color: var(--teal); color: var(--teal-dark); }

.danger-zone {
  margin-top: 24px;
  padding: 16px 20px;
  border: 1px dashed var(--bad);
  border-radius: var(--radius);
  background: #FBF3F1;
}
.danger-zone summary { cursor: pointer; font-weight: 600; color: var(--bad); font-family: 'Fraunces', serif; }
.danger-zone .hint { margin: 10px 0 14px; max-width: 640px; }
.danger-zone code { font-family: var(--mono); font-size: 12px; background: #F0E4E0; padding: 1px 5px; border-radius: 4px; }

/* Compose page */
.compose-header {
  display: flex;
  align-items: baseline;
  gap: 16px;
  margin-bottom: 20px;
  flex-wrap: wrap;
}
.back-link { color: var(--ink-soft); text-decoration: none; font-size: 13px; }
.compose-header h1 { flex: 1; font-size: 24px; }
.year-badge {
  display: inline-block;
  font-family: var(--mono);
  font-size: 12px;
  background: var(--teal);
  color: white;
  padding: 2px 8px;
  border-radius: 5px;
  vertical-align: middle;
  margin-left: 8px;
}

.topic-inline {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  background: var(--paper-raised);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px 14px;
  margin-bottom: 16px;
}
.topic-inline label {
  font-size: 12px;
  font-weight: 600;
  color: var(--ink-soft);
  text-transform: uppercase;
  letter-spacing: 0.03em;
  white-space: nowrap;
}
.topic-inline input[type=text] {
  flex: 1;
  min-width: 220px;
  border-color: transparent;
  background: #F4F1E9;
}
.topic-inline input[type=text]:focus { border-color: var(--teal); outline: none; }
.topic-inline .hint { flex-basis: 100%; margin: 2px 0 0; }

.roster {
  width: 100%;
  border-collapse: collapse;
  background: var(--paper-raised);
  border-radius: var(--radius);
  overflow: hidden;
  border: 1px solid var(--line);
}
.table-scroll {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  border-radius: var(--radius);
}
.table-scroll .roster {
  border-radius: 0;
}
.roster th {
  text-align: left;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--ink-soft);
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
}
.roster td {
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
  vertical-align: top;
}
.roster tr:last-child td { border-bottom: none; }
.roster-row.row-unmatched { background: #FBF6EC; }
.roster-row.row-sent { background: #EEF5F0; }
.student-name { font-weight: 600; white-space: nowrap; min-width: 190px; }
.col-check { width: 30px; }

.progress-bar-wrap {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 16px;
}
.progress-bar {
  flex: 1;
  max-width: 320px;
  height: 8px;
  border-radius: 4px;
  background: var(--line);
  overflow: hidden;
}
.progress-fill {
  height: 100%;
  background: var(--teal);
  transition: width 0.3s ease;
}
.progress-label {
  font-family: var(--mono);
  font-size: 12px;
  color: var(--ink-soft);
}

.regular-cell, .extra-cell { min-width: 400px; }
.regular-cell select, .regular-cell textarea { width: 100%; margin-bottom: 4px; display: block; }

.toggle-extra {
  background: none;
  border: 1px dashed var(--line);
  color: var(--ink-soft);
  font-size: 12px;
  font-weight: 600;
  padding: 4px 10px;
}
.toggle-extra:hover { border-color: var(--amber); color: var(--amber); }
.extra-form {
  display: none;
  margin-top: 8px;
  padding: 10px;
  background: #FBF9F3;
  border: 1px solid var(--line);
  border-radius: 8px;
}
.extra-form.extra-form-open { display: block; }
.extra-form select, .extra-form textarea { width: 100%; margin-bottom: 4px; display: block; }
.extra-form .hint { margin: 4px 0 0; font-size: 11px; }
.btn-send-extra {
  background: var(--amber);
  border-color: var(--amber);
}
.btn-send-extra:hover { background: #B87A2C; }

.badge {
  font-family: var(--mono);
  font-size: 11px;
  padding: 2px 7px;
  border-radius: 5px;
  display: inline-block;
  white-space: nowrap;
}
.badge-matched { background: #E9F3EC; color: var(--good); }
.badge-ambiguous { background: #FCF0DC; color: var(--amber); }
.badge-notfound { background: #FBEAE6; color: var(--bad); }
.manual-match { margin-top: 6px; display: flex; gap: 4px; }
.manual-match input { width: 70px; padding: 4px 6px; font-size: 12px; }
.manual-match button { padding: 4px 8px; font-size: 12px; }

.template-select, .category-select { min-width: 150px; }
.preview-text {
  width: 100%;
  max-width: 440px;
  resize: none;
  overflow-y: hidden;
  font-family: inherit;
  line-height: 1.5;
}
.btn-regenerate, .btn-regenerate-extra {
  font-size: 12px;
  padding: 5px 10px;
  margin-left: 4px;
}
.send-status { font-size: 12px; margin-left: 6px; }
.save-status { font-size: 12px; margin-left: 6px; }

/* Templates page */
.add-template {
  background: var(--paper-raised);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px 20px;
  margin: 16px 0 28px;
}
.head-form { margin-bottom: 14px; }
.head-search { width: 100%; max-width: 260px; }
.template-list { list-style: none; padding: 0; margin: 0 0 20px; }
.template-list li {
  padding: 8px 0;
  border-bottom: 1px solid var(--line);
  font-size: 14px;
}
.cat-tag {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--teal-dark);
  background: #E9F0EE;
  padding: 2px 6px;
  border-radius: 4px;
  margin-right: 8px;
}
.year-group-block {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--paper-raised);
  padding: 12px 18px;
  margin-bottom: 10px;
}
.year-group-block summary {
  cursor: pointer;
  font-weight: 600;
  font-family: 'Fraunces', serif;
}

/* Roster page: edit fields + match picker */
.roster input[type=text] { width: 160px; }
.match-search { width: 180px; margin: 4px 0; }
.btn-set-match { font-size: 12px; padding: 4px 10px; }
.btn-set-match:disabled { opacity: 0.4; cursor: not-allowed; }

.gender-filter-bar {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 14px;
  font-size: 14px;
}
.gender-filter-bar label { display: flex; align-items: center; gap: 6px; cursor: pointer; }

.photo-upload-section {
  background: var(--paper-raised);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px 20px;
  margin-bottom: 20px;
}
.photo-upload-section h3 { margin-bottom: 6px; }
.photo-upload-section .hint { max-width: 640px; }
#photo-upload-status { margin-left: 10px; }

.unmatched-photos-list {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  margin-top: 14px;
}
.unmatched-photo-card {
  display: flex;
  gap: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px;
  background: #FBF9F3;
  width: 280px;
}
.unmatched-photo-thumb {
  width: 56px;
  height: 56px;
  border-radius: 8px;
  background-size: cover;
  background-position: center;
  flex-shrink: 0;
  background-color: var(--line);
}
.unmatched-photo-info { flex: 1; min-width: 0; }
.unmatched-photo-filename {
  font-size: 12px;
  color: var(--ink-soft);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-bottom: 4px;
}
.unmatched-photo-search { width: 100%; font-size: 13px; padding: 5px 8px; margin-bottom: 4px; }
.unmatched-photo-actions { display: flex; gap: 6px; align-items: center; }
.btn-assign-photo { font-size: 12px; padding: 4px 10px; }
.btn-assign-photo:disabled { opacity: 0.4; cursor: not-allowed; }

/* ---------- Gender toggle ---------- */
.gender-toggle {
  display: inline-flex;
  gap: 2px;
  margin-left: 6px;
  vertical-align: middle;
}
.gender-btn {
  background: var(--paper);
  border: 1px solid var(--line);
  color: var(--ink-soft);
  font-weight: 400;
  font-size: 13px;
  padding: 1px 6px;
  border-radius: 5px;
  line-height: 1.4;
}
.gender-btn:hover { border-color: var(--teal); color: var(--teal-dark); }
.gender-btn.active {
  background: var(--teal);
  border-color: var(--teal);
  color: white;
  font-weight: 600;
}

/* ---------- Student photos ---------- */
.student-photo-row {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 6px;
}
.student-name-text { font-weight: 600; }
.student-photo-wrap {
  position: relative;
  width: 100%;
  max-width: 160px;
  aspect-ratio: 3 / 4;
  margin-top: 2px;
}
.student-photo {
  width: 100%;
  height: 100%;
  border-radius: 10px;
  background-size: cover;
  background-position: center;
  background-color:
