:root {
  --bg: #f6f7f3;
  --surface: #ffffff;
  --surface-soft: #eef3f0;
  --ink: #17211d;
  --muted: #63706a;
  --line: #d9e0dc;
  --primary: #176b61;
  --primary-strong: #0f4f48;
  --accent: #c4493f;
  --warn: #a76813;
  --ok: #24764b;
  --bad: #b73535;
  --radius: 8px;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  min-height: 100vh;
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  border-radius: var(--radius);
  min-height: 38px;
  padding: 8px 12px;
  cursor: pointer;
}

button i {
  width: 1em;
  text-align: center;
  font-size: 0.92em;
}

button:hover {
  border-color: var(--primary);
}

button.primary,
.primary {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}

button.primary:hover,
.primary:hover {
  background: var(--primary-strong);
}

button.ghost {
  background: transparent;
}

button.danger {
  color: var(--bad);
  border-color: #e5c4c4;
  background: #fff8f8;
}

button.link {
  border: 0;
  background: transparent;
  padding: 0;
  min-height: 0;
  color: var(--primary);
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px 11px;
  background: #fff;
  color: var(--ink);
}

textarea {
  resize: vertical;
}

label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 650;
}

label.check {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--ink);
  font-weight: 500;
}

label.check input {
  flex: 0 0 18px;
  width: 18px;
  min-width: 18px;
  height: 18px;
  margin: 0;
  accent-color: var(--primary);
}

hr {
  border: 0;
  border-top: 1px solid var(--line);
}

.app-shell {
  width: min(1440px, calc(100vw - 32px));
  margin: 0 auto;
  min-height: 100vh;
}

.topbar {
  height: 72px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  border-bottom: 1px solid var(--line);
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  border: 0;
  background: transparent;
  padding: 0;
}

.brand strong {
  display: block;
  font-size: 18px;
}

.brand small {
  display: block;
  color: var(--muted);
  margin-top: 1px;
}

.brand-mark {
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  border-radius: var(--radius);
  background: var(--ink);
  color: #fff;
  font-weight: 800;
  font-size: 17px;
}

.nav {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.nav button.active {
  background: var(--surface-soft);
  border-color: #bfd0c9;
  color: var(--primary);
}

.main {
  padding: 28px 0 56px;
}

.toolbar {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 20px;
  margin-bottom: 20px;
}

h1,
h2 {
  margin: 0;
  letter-spacing: 0;
}

h1 {
  font-size: 30px;
  line-height: 1.15;
}

h2 {
  font-size: 18px;
  margin: 0 0 10px;
}

p {
  margin: 6px 0 0;
  color: var(--muted);
}

.actions {
  display: flex;
  align-items: end;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.compact-select {
  min-width: 220px;
}

.panel,
.auth-panel,
.table-wrap {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

.panel {
  padding: 16px;
  margin-bottom: 16px;
}

.compact-panel {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
  align-items: end;
}

.auth-panel {
  width: min(440px, 100%);
  margin: 10vh auto 0;
  padding: 24px;
}

.auth-panel.success {
  text-align: center;
}

.form-grid {
  display: grid;
  gap: 14px;
  margin-top: 18px;
}

.inline-form {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  align-items: end;
  gap: 12px;
}

.week-settings-form {
  display: grid;
  grid-template-columns: repeat(4, minmax(170px, 1fr));
  gap: 14px 18px;
  align-items: end;
}

.week-settings-form label.check {
  min-height: 42px;
  align-self: end;
}

.split {
  display: grid;
  grid-template-columns: minmax(360px, 0.8fr) minmax(420px, 1.2fr);
  gap: 22px;
  align-items: start;
}

.list {
  display: grid;
  gap: 10px;
}

.row-card {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px;
}

.row-card .row-main {
  flex: 1;
  text-align: left;
  border: 0;
  background: transparent;
  padding: 4px;
}

.row-main strong {
  display: block;
  font-size: 16px;
}

.week-card .row-main {
  display: grid;
  grid-template-columns: max-content minmax(0, 1fr);
  align-items: center;
  gap: 12px;
}

.week-card-title {
  white-space: nowrap;
}

.row-main span,
.muted {
  color: var(--muted);
  font-size: 13px;
}

.muted-row {
  opacity: 0.58;
}

.hidden {
  display: none;
}

.empty {
  padding: 22px;
  border: 1px dashed var(--line);
  border-radius: var(--radius);
  color: var(--muted);
  text-align: center;
}

.table-wrap {
  overflow: auto;
}

.table-wrap.tight {
  max-height: 58vh;
}

table {
  width: 100%;
  border-collapse: collapse;
  min-width: 720px;
}

.enrolment-table {
  table-layout: fixed;
  min-width: 0;
}

.enrolment-table th:nth-child(1),
.enrolment-table td:nth-child(1) {
  width: 28%;
}

.enrolment-table th:nth-child(3),
.enrolment-table td:nth-child(3) {
  width: 270px;
}

th,
td {
  padding: 12px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: top;
}

.row-actions {
  display: flex;
  gap: 6px;
  justify-content: flex-end;
  white-space: nowrap;
}

td.row-actions {
  display: table-cell;
}

.row-action-buttons {
  display: flex;
  gap: 6px;
  justify-content: flex-end;
  white-space: nowrap;
}

.row-actions button,
.row-action-buttons button {
  flex: 0 0 auto;
}

th {
  font-size: 12px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0;
  background: var(--surface-soft);
}

td small {
  color: var(--muted);
}

.late-time {
  color: var(--bad) !important;
  font-weight: 800;
}

tr.flagged {
  background: #fffaf1;
}

.student-expanded-row {
  display: none;
}

#week-attendance-table.show-expanded .student-expanded-row {
  display: table-row;
}

.expanded-detail {
  display: grid;
  grid-template-columns: repeat(5, minmax(160px, 1fr));
  gap: 8px 14px;
  padding: 10px 0;
}

.expanded-detail + .expanded-detail {
  border-top: 1px solid var(--line);
}

.override-detail {
  background: #fff8ea;
  border: 1px solid #efd8a8;
  border-radius: var(--radius);
  padding: 10px;
}

code {
  background: var(--surface-soft);
  border: 1px solid var(--line);
  border-radius: 5px;
  padding: 1px 5px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 12px;
}

.pill {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  border-radius: 999px;
  padding: 2px 9px;
  font-size: 12px;
  font-weight: 750;
  background: var(--surface-soft);
  color: var(--muted);
  margin: 2px 4px 2px 0;
}

.pill.ok {
  background: #eaf6ef;
  color: var(--ok);
}

.pill.bad {
  background: #faeeee;
  color: var(--bad);
}

.pill.warn,
.toast.warn {
  background: #fff3db;
  color: var(--warn);
}

.toasts {
  position: fixed;
  top: 16px;
  right: 16px;
  display: grid;
  gap: 8px;
  z-index: 10;
}

.toast {
  padding: 10px 12px;
  background: #eaf6ef;
  color: var(--ok);
  border: 1px solid currentColor;
  border-radius: var(--radius);
  max-width: 340px;
}

.toast.bad {
  background: #faeeee;
  color: var(--bad);
}

.form-error {
  border: 1px solid #e5c4c4;
  border-radius: var(--radius);
  background: #fff8f8;
  color: var(--bad);
  padding: 10px 12px;
  font-weight: 700;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(23, 33, 29, 0.35);
}

.modal {
  width: min(920px, 100%);
  max-height: min(820px, calc(100vh - 48px));
  overflow: auto;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px;
  box-shadow: 0 20px 70px rgba(23, 33, 29, 0.22);
}

.modal.narrow-modal {
  width: min(520px, 100%);
}

.modal-header {
  display: flex;
  align-items: start;
  justify-content: space-between;
  gap: 16px;
  border-bottom: 1px solid var(--line);
  padding-bottom: 12px;
  margin-bottom: 14px;
}

.detail-block {
  padding: 12px 0;
}

.detail-block + .detail-block {
  border-top: 1px solid var(--line);
}

.detail-block h3 {
  margin: 0 0 10px;
  font-size: 15px;
}

.detail-grid {
  display: grid;
  grid-template-columns: 150px minmax(0, 1fr);
  gap: 8px 12px;
  margin: 0;
}

.detail-grid dt {
  color: var(--muted);
  font-weight: 750;
}

.detail-grid dd {
  margin: 0;
  overflow-wrap: anywhere;
}

.public-qr {
  height: 100vh;
  overflow: hidden;
  display: grid;
  grid-template-columns: minmax(560px, 58vw) 1fr;
  background: #fff;
  color: var(--ink);
}

.qr-pane {
  display: grid;
  place-items: center;
  background: var(--surface-soft);
  padding: clamp(28px, 4vw, 64px);
  min-height: 0;
}

.qr-click-target {
  width: min(620px, 48vw, calc(100vh - 112px));
  min-height: min(620px, 48vw, calc(100vh - 112px));
  display: grid;
  place-items: center;
  border: 2px solid var(--ink);
  border-radius: var(--radius);
  background: #fff;
  color: var(--primary);
  text-decoration: none;
  padding: 20px;
}

.qr-click-target:hover {
  border-color: var(--primary);
}

#qr-canvas {
  width: min(580px, 44vw, calc(100vh - 156px)) !important;
  height: min(580px, 44vw, calc(100vh - 156px)) !important;
}

#qr-open-label {
  display: none;
  font-size: clamp(24px, 4vw, 44px);
  font-weight: 800;
  text-align: center;
}

.qr-click-target.qr-unavailable #qr-canvas {
  display: none;
}

.qr-click-target.qr-unavailable #qr-open-label {
  display: block;
}

#qr-fallback {
  display: none;
  max-width: 620px;
  overflow-wrap: anywhere;
  text-align: center;
  font-size: 14px;
}

.qr-info {
  position: relative;
  display: block;
  min-height: 0;
  padding: clamp(24px, 4vw, 56px);
}

.qr-info h1 {
  position: absolute;
  right: clamp(24px, 4vw, 56px);
  bottom: clamp(70px, 8vh, 120px);
  left: clamp(24px, 4vw, 56px);
  margin: 0;
  font-size: clamp(24px, 3.2vw, 48px);
  line-height: 1.05;
  max-width: none;
}

.qr-info p {
  position: absolute;
  right: clamp(24px, 4vw, 56px);
  bottom: clamp(32px, 4vh, 64px);
  left: clamp(24px, 4vw, 56px);
  margin: 0;
  font-size: clamp(16px, 1.8vw, 26px);
  color: var(--primary);
}

.live-attendance {
  position: absolute;
  top: clamp(24px, 4vw, 56px);
  right: clamp(24px, 4vw, 56px);
  width: min(420px, calc(100% - 48px));
  height: min(58vh, 520px);
  overflow: hidden;
}

.live-attendance-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  border-bottom: 1px solid var(--line);
  padding-bottom: 8px;
  margin-bottom: 8px;
  color: var(--muted);
  font-size: clamp(12px, 1vw, 15px);
  text-transform: uppercase;
}

.live-attendance-header span {
  display: inline-grid;
  place-items: center;
  min-width: 28px;
  min-height: 28px;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
}

#live-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin: 0;
  padding: 0;
  font-size: clamp(15px, 1.45vw, 23px);
  height: calc(min(58vh, 520px) - 48px);
  overflow: hidden;
}

#live-list li {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--muted);
  padding: 2px 0;
}

#live-list li.is-flagged {
  text-decoration: underline;
  text-decoration-thickness: 2px;
  text-underline-offset: 0.16em;
}

.muted-live {
  opacity: 0.72;
}

.public-form {
  min-height: 100vh;
  display: grid;
  place-items: start center;
  padding: 32px 16px;
}

.consent {
  align-items: start;
}

@media (max-width: 900px) {
  .topbar,
  .toolbar {
    align-items: stretch;
    flex-direction: column;
  }

  .nav,
  .actions {
    justify-content: flex-start;
  }

  .inline-form,
  .week-settings-form,
  .compact-panel,
  .split {
    grid-template-columns: 1fr;
  }

  .row-card {
    align-items: stretch;
    flex-direction: column;
  }

  .expanded-detail,
  .detail-grid {
    grid-template-columns: 1fr;
  }

  .modal-header {
    flex-direction: column;
  }

  .public-qr {
    grid-template-columns: 1fr;
    grid-template-rows: 58vh 42vh;
  }

  .qr-info h1 {
    left: 24px;
    right: 24px;
    bottom: 54px;
    max-width: none;
    font-size: 30px;
  }

  .qr-info p {
    left: 24px;
    right: 24px;
    bottom: 24px;
  }

  .live-attendance {
    top: 20px;
    right: 24px;
    height: 20vh;
  }

  #live-list {
    height: calc(20vh - 48px);
  }
}
