:root {
  --bg: #f6f3ec;
  --surface: #ffffff;
  --surface-2: #faf8f3;
  --border: #e4ded1;
  --text: #22201c;
  --muted: #6d675c;
  --accent: #3b3a8f;
  --accent-ink: #ffffff;
  --good: #1b7f45;
  --bad: #b3261e;
  --gold: #e0a400;
  --radius: 16px;
  --shadow: 0 1px 2px rgba(40, 30, 10, .06), 0 4px 16px rgba(40, 30, 10, .06);
  --display: 'Fredoka', ui-rounded, system-ui, sans-serif;
  --body: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17161a;
    --surface: #222127;
    --surface-2: #1c1b20;
    --border: #36343c;
    --text: #f1eee8;
    --muted: #a8a399;
    --accent: #9d9cf0;
    --accent-ink: #15142b;
    --good: #5fd08d;
    --bad: #ff8a80;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 16px rgba(0, 0, 0, .25);
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--body);
  font-size: 16px;
  line-height: 1.45;
}

h1, h2, h3 { font-family: var(--display); font-weight: 600; line-height: 1.15; margin: 0; }

a { color: var(--accent); }

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

.hidden { display: none !important; }

.muted { color: var(--muted); }

.good { color: var(--good); }

.bad { color: var(--bad); }

.wrap { max-width: 1180px; margin: 0 auto; padding: 0 16px; }

/* ---------- Buttons & inputs ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: .4em;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 999px;
  padding: .5em 1.05em;
  cursor: pointer;
  font-weight: 600;
  text-decoration: none;
  color: var(--text);
  white-space: nowrap;
  transition: transform .08s ease, background .15s ease, border-color .15s ease;
}
.btn:hover { border-color: var(--muted); }
.btn:active { transform: scale(.97); }
.btn:disabled { opacity: .45; cursor: not-allowed; transform: none; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.btn.danger { color: var(--bad); }
.btn.small { padding: .25em .7em; font-size: .875rem; }
.btn.big { font-size: 1.15rem; padding: .8em 1.4em; }

input[type=text], input[type=password], input[type=number], input[type=search], select, textarea {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: .55em .75em;
  min-width: 0;
}
input:focus, select:focus, textarea:focus, .btn:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--accent) 40%, transparent);
  outline-offset: 1px;
}
textarea { width: 100%; resize: vertical; }

label.field { display: grid; gap: .3em; font-weight: 600; font-size: .9rem; }
label.field > span { color: var(--muted); }

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow);
}

/* ---------- Public scoreboard ---------- */

.board-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 24px 0 8px;
}
.board-head h1 { font-size: clamp(1.8rem, 4vw, 3rem); }
.board-head .sub { color: var(--muted); margin-top: 4px; }

.board {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 18px;
  margin: 20px 0 32px;
}

.house-card {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--border);
  border-top: 8px solid var(--house);
  border-radius: var(--radius);
  padding: 22px 18px 18px;
  text-align: center;
  box-shadow: var(--shadow);
  overflow: hidden;
}
.house-card::before {
  content: '';
  position: absolute;
  inset: 0;
  background: radial-gradient(circle at 50% 0%, color-mix(in srgb, var(--house) 16%, transparent), transparent 65%);
  pointer-events: none;
}
.house-card .crest { position: relative; filter: drop-shadow(0 4px 8px rgba(0,0,0,.18)); }
.house-card h2 { font-size: 1.45rem; margin-top: 10px; position: relative; }
.house-card .total {
  position: relative;
  font-family: var(--display);
  font-size: clamp(2.6rem, 5vw, 3.6rem);
  font-weight: 700;
  line-height: 1;
  margin: 10px 0 2px;
  font-variant-numeric: tabular-nums;
}
.house-card .label { color: var(--muted); font-size: .85rem; position: relative; }
.house-card .bar {
  position: relative;
  height: 10px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  margin: 14px 0 10px;
  overflow: hidden;
}
.house-card .bar > i {
  display: block;
  height: 100%;
  background: var(--house);
  border-radius: 999px;
  transition: width .8s cubic-bezier(.2,.8,.2,1);
}
.house-card .week { position: relative; font-size: .9rem; font-weight: 600; }
.rank {
  position: absolute;
  top: 10px;
  left: 12px;
  font-family: var(--display);
  font-weight: 600;
  font-size: .95rem;
  color: var(--muted);
}
.house-card.leader .rank { color: var(--gold); }
.house-card.leader { box-shadow: 0 0 0 3px color-mix(in srgb, var(--gold) 55%, transparent), var(--shadow); }
.house-card.bump .total { animation: bump .6s ease; }
@keyframes bump { 30% { transform: scale(1.15); } }

.feed h2 { font-size: 1.35rem; margin-bottom: 12px; }
.feed-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.feed-list li {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 6px solid var(--house);
  border-radius: 12px;
  padding: 10px 14px;
}
.feed-list .pts { font-family: var(--display); font-weight: 700; font-size: 1.2rem; min-width: 3.2em; }
.feed-list .what { flex: 1; min-width: 0; }
.feed-list .when { color: var(--muted); font-size: .85rem; white-space: nowrap; }

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

footer.foot { color: var(--muted); font-size: .85rem; padding: 24px 0 40px; text-align: center; }

/* ---------- Teacher ---------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.topbar .wrap { display: flex; align-items: center; gap: 12px; padding-top: 10px; padding-bottom: 10px; flex-wrap: wrap; }
.topbar h1 { font-size: 1.3rem; margin-right: auto; }
.tabs { display: flex; gap: 4px; overflow-x: auto; scrollbar-width: none; }
.tabs button {
  border: 0;
  background: transparent;
  padding: .5em .9em;
  border-radius: 999px;
  cursor: pointer;
  font-weight: 600;
  color: var(--muted);
  white-space: nowrap;
}
.tabs button[aria-selected=true] { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

main.teacher { padding: 20px 16px 60px; }
.section-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; flex-wrap: wrap; }
.section-head h2 { font-size: 1.5rem; }

.award-grid { display: grid; grid-template-columns: minmax(0, 1.3fr) minmax(0, 1fr); gap: 18px; align-items: start; }
@media (max-width: 860px) { .award-grid { grid-template-columns: minmax(0, 1fr); } }

.step { font-size: 1.1rem; margin-bottom: 12px; }
.step + .step, .panel .step:not(:first-child) { margin-top: 22px; }

.house-picks { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 10px; }
.house-pick {
  display: grid;
  justify-items: center;
  gap: 4px;
  padding: 12px 8px;
  background: var(--surface-2);
  border: 2px solid var(--border);
  border-radius: 14px;
  cursor: pointer;
  transition: border-color .15s, background .15s, transform .08s;
}
.house-pick:active { transform: scale(.97); }
.house-pick .name { font-weight: 700; font-family: var(--display); font-size: 1.05rem; text-align: center; }
.house-pick .sum { font-size: .85rem; color: var(--muted); }
.house-pick[aria-pressed=true] {
  border-color: var(--house);
  background: color-mix(in srgb, var(--house) 14%, var(--surface));
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--house) 30%, transparent);
}

.or { display: flex; align-items: center; gap: 10px; color: var(--muted); margin: 18px 0 12px; font-size: .9rem; }
.or::before, .or::after { content: ''; flex: 1; height: 1px; background: var(--border); }

.picker { position: relative; }
.picker input { width: 100%; }
.results {
  position: absolute;
  left: 0; right: 0; top: calc(100% + 4px);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  max-height: 300px;
  overflow-y: auto;
  z-index: 5;
}
.results button {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  border: 0;
  background: transparent;
  text-align: left;
  padding: 9px 12px;
  cursor: pointer;
}
.results button:hover, .results button.active { background: var(--surface-2); }
.results .meta { margin-left: auto; color: var(--muted); font-size: .85rem; }

.dot { width: 12px; height: 12px; border-radius: 50%; background: var(--house, var(--border)); flex: none; display: inline-block; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 6px 4px 10px;
  border-radius: 999px;
  background: color-mix(in srgb, var(--house, var(--muted)) 15%, var(--surface));
  border: 1px solid color-mix(in srgb, var(--house, var(--muted)) 45%, transparent);
  font-size: .9rem;
  font-weight: 600;
}
.chip button { border: 0; background: transparent; cursor: pointer; padding: 0 4px; color: var(--muted); font-size: 1.1rem; line-height: 1; }
.chip.suggestion { cursor: pointer; padding: 4px 12px; --house: var(--accent); font-weight: 500; }

.amounts { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.amounts .btn { font-family: var(--display); font-size: 1.15rem; padding: .6em 0; }
.amounts .btn.plus[aria-pressed=true] { background: var(--good); color: #fff; border-color: transparent; }
.amounts .btn.minus { color: var(--bad); }
.amounts .btn.minus[aria-pressed=true] { background: var(--bad); color: #fff; border-color: transparent; }
.custom-amt { display: flex; gap: 8px; align-items: center; margin-top: 10px; }
.custom-amt input { width: 120px; }

#reason { width: 100%; }
#award-submit { width: 100%; margin-top: 22px; }
.summary-line { margin-top: 10px; color: var(--muted); font-size: .9rem; text-align: center; min-height: 1.4em; }

.mini-feed { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.mini-feed li { display: flex; gap: 10px; align-items: center; padding: 8px 0; border-bottom: 1px solid var(--border); font-size: .92rem; }
.mini-feed li:last-child { border-bottom: 0; }
.mini-feed .pts { font-weight: 700; min-width: 3em; font-family: var(--display); }
.mini-feed .what { flex: 1; min-width: 0; }

.toolbar { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; margin-bottom: 14px; }
.toolbar input[type=search] { flex: 1 1 220px; }

.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }
table { width: 100%; border-collapse: collapse; font-size: .95rem; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { font-size: .8rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); background: var(--surface-2); }
tr:last-child td { border-bottom: 0; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
td.wrap-text { white-space: normal; min-width: 180px; }
td select { padding: .3em .5em; }
.house-tag { display: inline-flex; align-items: center; gap: 6px; font-weight: 600; }

.form-row { display: flex; gap: 10px; flex-wrap: wrap; align-items: end; }
.form-row > .field { flex: 1 1 160px; }

details.panel summary { cursor: pointer; font-weight: 600; }
details.panel[open] summary { margin-bottom: 12px; }

.stack { display: grid; gap: 18px; }

.house-edit-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 16px; }
.house-edit { display: grid; gap: 12px; justify-items: center; text-align: center; }
.house-edit .field { width: 100%; text-align: left; }
.swatches { display: flex; flex-wrap: wrap; gap: 6px; justify-content: center; }
.swatch { width: 28px; height: 28px; border-radius: 50%; border: 2px solid var(--surface); box-shadow: 0 0 0 1px var(--border); cursor: pointer; padding: 0; }
.swatch[aria-pressed=true] { box-shadow: 0 0 0 3px var(--text); }
.animal-picks { display: grid; grid-template-columns: repeat(auto-fill, minmax(38px, 1fr)); gap: 4px; }
.animal-pick {
  aspect-ratio: 1;
  font-size: 1.35rem;
  line-height: 1;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2);
  cursor: pointer;
  padding: 0;
  transition: transform .08s, border-color .15s;
}
.animal-pick:hover { border-color: var(--muted); }
.animal-pick:active { transform: scale(.92); }
.animal-pick.letter { font-family: var(--display); font-weight: 700; font-size: .95rem; color: var(--muted); }
.animal-pick[aria-pressed=true] {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 14%, var(--surface));
  box-shadow: 0 0 0 2px var(--accent);
}

input[type=color] { width: 100%; height: 40px; border: 1px solid var(--border); border-radius: 10px; background: var(--surface-2); padding: 3px; cursor: pointer; }

.name-modes { border: 0; padding: 0; margin: 0; display: grid; gap: 8px; }
.name-modes legend { font-weight: 600; padding: 0; margin-bottom: 2px; }
.name-modes p { margin: 0 0 4px; font-size: .9rem; }
.name-modes label {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface-2);
  cursor: pointer;
}
.name-modes label:has(input:checked) { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 8%, var(--surface)); }
.name-modes input { margin-top: 4px; accent-color: var(--accent); }
.name-modes label > span .muted { display: block; font-size: .9rem; }

.banner {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  justify-content: space-between;
  padding: 12px 16px;
  margin-bottom: 18px;
  border-radius: 12px;
  border: 1px solid color-mix(in srgb, var(--gold) 55%, transparent);
  background: color-mix(in srgb, var(--gold) 14%, var(--surface));
  font-weight: 500;
}
.banner.bad-banner {
  border-color: color-mix(in srgb, var(--bad) 55%, transparent);
  background: color-mix(in srgb, var(--bad) 12%, var(--surface));
}

.auth-card { max-width: 420px; margin: 8vh auto; display: grid; gap: 14px; }
.auth-card h1 { font-size: 1.8rem; }
.auth-card .btn { width: 100%; }
.error { color: var(--bad); font-weight: 600; min-height: 1.2em; }

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%) translateY(20px);
  opacity: 0;
  pointer-events: none;
  background: var(--text);
  color: var(--bg);
  padding: 12px 14px 12px 18px;
  border-radius: 14px;
  display: flex;
  gap: 14px;
  align-items: center;
  box-shadow: 0 8px 30px rgba(0,0,0,.25);
  transition: opacity .2s, transform .2s;
  max-width: calc(100vw - 32px);
  z-index: 50;
}
.toast.show { opacity: 1; transform: translateX(-50%); pointer-events: auto; }
.toast.err { background: var(--bad); color: #fff; }
.toast button { background: transparent; border: 1px solid currentColor; color: inherit; border-radius: 999px; padding: .2em .8em; cursor: pointer; font-weight: 700; }

@media (max-width: 600px) {
  .board-head { flex-direction: column; align-items: flex-start; }
  .amounts .btn { font-size: 1rem; }
  .panel { padding: 16px; }
}
