:root {
  --bg: #e9eef4;
  --paper: #ffffff;
  --ink: #12203a;
  --muted: #5b6a85;
  --line: #d3dbe6;
  --ticket: #ffc92e;
  --ticket-ink: #3a2a00;
  --plus: #12805c;
  --minus: #b83b32;
  --gA: #3b6fd4; --gB: #0f8f84; --gC: #c8702a; --gD: #8a4fc7;
  --radius: 14px;
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", "Apple SD Gothic Neo", "Malgun Gothic", sans-serif;
}
[hidden] { display: none !important; }
h1, h2 { margin: 0; line-height: 1.2; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 3px solid #3b6fd4; outline-offset: 2px; }

/* ---------- top bar ---------- */
.top { position: sticky; top: 0; z-index: 5; background: var(--ink); color: #fff; }
.top > * { max-width: 760px; margin: 0 auto; }
header { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 12px 16px 6px; }
header h1 { font-size: 1.15rem; letter-spacing: .01em; }
header p { margin: 0; font-size: .8rem; color: #b8c4dc; }
.sync { font-size: .78rem; padding: 3px 10px; border-radius: 99px; background: #23365a; color: #cfe0ff; white-space: nowrap; }
.sync.bad { background: var(--minus); color: #fff; }

.tabs { display: flex; gap: 4px; padding: 6px 12px 0; }
.tabs button {
  flex: 1; border: 0; background: transparent; color: #b8c4dc;
  padding: 10px 4px; font-weight: 600; border-bottom: 3px solid transparent;
}
.tabs button.active { color: #fff; border-bottom-color: var(--ticket); }

.tools { background: #1b2d50; max-width: none !important; }
.tools > * { max-width: 760px; margin: 0 auto; }
.chips { display: flex; gap: 8px; padding: 10px 16px; overflow-x: auto; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chips.groups { padding-top: 0; }
.chip {
  flex: none; border: 1.5px solid #4a6190; background: transparent; color: #dbe6ff;
  border-radius: 99px; padding: 7px 14px; font-size: .9rem; min-height: 38px;
}
.chip.on { background: var(--ticket); border-color: var(--ticket); color: var(--ticket-ink); font-weight: 700; }

/* ---------- layout ---------- */
main { max-width: 760px; margin: 0 auto; padding: 12px 12px 48px; }
.group-head { margin: 18px 4px 8px; font-weight: 700; color: var(--muted); display: flex; align-items: center; gap: 8px; }
.group-head:first-child { margin-top: 6px; }
.tag {
  display: inline-grid; place-items: center; width: 24px; height: 24px; border-radius: 7px;
  color: #fff; font-size: .8rem; font-weight: 700; background: var(--muted);
}
.tag.A { background: var(--gA); } .tag.B { background: var(--gB); } .tag.C { background: var(--gC); } .tag.D { background: var(--gD); }

ul { list-style: none; margin: 0; padding: 0; }

/* ---------- student rows ---------- */
.student {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  background: var(--paper); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 8px 8px 8px 14px; margin-bottom: 8px;
}
.name { font-weight: 600; font-size: 1.05rem; min-width: 0; overflow-wrap: anywhere; }
.counter { display: flex; align-items: center; gap: 8px; flex: none; }
.btn {
  width: 52px; height: 52px; border-radius: 12px; border: 2px solid; background: var(--paper);
  font-size: 1.7rem; line-height: 1; display: grid; place-items: center; touch-action: manipulation;
}
.btn.plus { background: var(--plus); border-color: var(--plus); color: #fff; }
.btn.minus { border-color: var(--minus); color: var(--minus); }
.btn:active { transform: scale(.94); }
.btn:disabled { opacity: .3; cursor: default; transform: none; }

/* the card total looks like a small ticket with notched sides */
.total {
  position: relative; min-width: 56px; height: 44px; padding: 0 10px; display: grid; place-items: center;
  background: var(--ticket); color: var(--ticket-ink); font-weight: 800; font-size: 1.5rem;
  font-variant-numeric: tabular-nums; border-radius: 6px;
}
.total::before, .total::after {
  content: ""; position: absolute; top: 50%; width: 10px; height: 10px; margin-top: -5px;
  background: var(--paper); border-radius: 50%;
}
.total::before { left: -5px; } .total::after { right: -5px; }

/* ---------- leaderboard ---------- */
.winners { display: grid; gap: 10px; margin-bottom: 18px; }
.winner {
  display: flex; align-items: center; gap: 14px; background: var(--paper); border-radius: var(--radius);
  padding: 14px; border: 2px solid var(--line);
}
.winner.r1 { border-color: #e0a800; background: #fff8dc; }
.winner.r2 { border-color: #9aa5b8; }
.winner.r3 { border-color: #c98a5a; }
.medal { font-size: 2rem; line-height: 1; }
.winner .name { font-size: 1.2rem; flex: 1; }
.winner .total { font-size: 1.7rem; height: 50px; min-width: 64px; }
.winners-title { margin: 4px 4px 10px; font-size: 1.05rem; }
.note { margin: 0 4px 12px; color: var(--muted); font-size: .9rem; }
.rest { display: flex; align-items: center; gap: 12px; background: var(--paper); border: 1px solid var(--line); border-radius: 12px; padding: 10px 14px; margin-bottom: 6px; }
.rank { width: 28px; color: var(--muted); font-weight: 700; font-variant-numeric: tabular-nums; }
.rest .name { flex: 1; font-weight: 500; }
.rest .num { font-weight: 800; font-size: 1.15rem; font-variant-numeric: tabular-nums; }
.name .tag { margin-left: 6px; width: 20px; height: 20px; font-size: .7rem; vertical-align: 1px; }

/* ---------- manage ---------- */
.panel { background: var(--paper); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin-bottom: 14px; }
.panel h2 { font-size: 1.1rem; margin-bottom: 10px; }
label { display: block; font-weight: 600; font-size: .9rem; margin: 10px 0 4px; }
input, select, textarea {
  width: 100%; padding: 10px 12px; border: 1.5px solid var(--line); border-radius: 10px; background: #fff; min-height: 44px;
}
.row { display: flex; gap: 12px; } .row > div { flex: 1; }
.hint { color: var(--muted); font-size: .88rem; margin: 8px 0; }
.error { color: var(--minus); min-height: 1.4em; margin: 8px 0; font-size: .9rem; }
button.primary, button.danger, #editCancel {
  min-height: 46px; padding: 0 18px; border-radius: 10px; border: 2px solid var(--ink); background: var(--paper); font-weight: 700;
}
button.primary { background: var(--ink); color: #fff; }
button.danger { border-color: var(--minus); color: var(--minus); }
button.link { background: none; border: 0; color: var(--muted); text-decoration: underline; padding: 10px 0; }
.panel .primary { margin-top: 6px; }

.log li { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 10px 0; border-top: 1px solid var(--line); }
.log li:first-child { border-top: 0; }
.log .what { min-width: 0; }
.log small { display: block; color: var(--muted); }
.log .undone .what { text-decoration: line-through; color: var(--muted); }
.log button { min-height: 40px; padding: 0 14px; border-radius: 9px; border: 1.5px solid var(--ink); background: var(--paper); font-weight: 600; flex: none; }
.log button:disabled { opacity: .35; }
.plusn { color: var(--plus); font-weight: 700; } .minusn { color: var(--minus); font-weight: 700; }

/* ---------- toast & dialog ---------- */
.toast {
  position: fixed; left: 50%; bottom: 18px; transform: translateX(-50%); z-index: 20;
  display: flex; align-items: center; gap: 14px; background: var(--ink); color: #fff;
  padding: 10px 10px 10px 16px; border-radius: 12px; max-width: calc(100% - 24px); box-shadow: 0 6px 24px rgba(18,32,58,.35);
}
.toast button { border: 0; background: var(--ticket); color: var(--ticket-ink); font-weight: 700; border-radius: 8px; padding: 8px 14px; min-height: 40px; }
dialog { border: 0; border-radius: 16px; padding: 20px; width: min(420px, calc(100% - 24px)); color: var(--ink); }
dialog::backdrop { background: rgba(18,32,58,.55); }
.dialog-actions { display: flex; align-items: center; gap: 8px; margin-top: 18px; flex-wrap: wrap; }
.spacer { flex: 1; }

/* ---------- login ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 20px; background: var(--ink); }
.login-box { background: var(--paper); border-radius: 18px; padding: 26px; width: min(380px, 100%); }
.login-box h1 { font-size: 1.4rem; }
.login-box > p { color: var(--muted); margin: 4px 0 14px; }
.login-box .primary { width: 100%; }

.empty { text-align: center; padding: 40px 16px; color: var(--muted); }
.empty button { margin-top: 10px; }

@media (min-width: 700px) {
  header h1 { font-size: 1.3rem; }
  .student { padding: 10px 10px 10px 18px; }
}
@media (prefers-reduced-motion: reduce) { .btn:active { transform: none; } }

/* ---------- leaderboard: Overall / Teams ---------- */
.seg { display: flex; gap: 4px; background: #d3dbe6; padding: 4px; border-radius: 12px; margin-bottom: 14px; }
.seg button { flex: 1; border: 0; background: transparent; border-radius: 9px; min-height: 42px; font-weight: 700; color: var(--muted); }
.seg button.on { background: var(--paper); color: var(--ink); box-shadow: 0 1px 3px rgba(18,32,58,.15); }

/* ---------- points breakdown ---------- */
.who { flex: 1; min-width: 0; display: block; }
.pills { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 5px; }
.pill { font-size: .72rem; font-weight: 700; padding: 2px 8px; border-radius: 99px; background: #e3f3ec; color: #0d6446; white-space: nowrap; }
.pill.neg { background: #fbe5e3; color: #962e27; }

/* ---------- teams view ---------- */
.team { --gc: var(--muted); background: var(--paper); border: 1px solid var(--line); border-left: 8px solid var(--gc); border-radius: var(--radius); margin-bottom: 12px; overflow: hidden; }
.team.A { --gc: var(--gA); } .team.B { --gc: var(--gB); } .team.C { --gc: var(--gC); } .team.D { --gc: var(--gD); }
.team.r1 { box-shadow: 0 0 0 2px #e0a800; }
.team-head { display: flex; align-items: center; gap: 10px; width: 100%; border: 0; background: transparent; padding: 14px 12px 12px 10px; text-align: left; min-height: 72px; }
.team-pos { width: 30px; text-align: center; font-size: 1.6rem; line-height: 1; font-weight: 800; color: var(--muted); }
.team-badge { flex: none; width: 46px; height: 46px; border-radius: 12px; background: var(--gc); color: #fff; display: grid; place-items: center; font-size: 1.5rem; font-weight: 800; }
.team-name { flex: 1; min-width: 0; font-weight: 800; font-size: 1.15rem; }
.team-name small { display: block; font-weight: 500; font-size: .82rem; color: var(--muted); }
.team-head .total { flex: none; height: 52px; min-width: 66px; font-size: 1.55rem; line-height: 1; align-content: center; }
.team-head .total small { display: block; font-size: .62rem; font-weight: 700; letter-spacing: .06em; text-transform: uppercase; margin-top: 2px; }
.chev { color: var(--muted); transition: transform .15s; }
.team.open .chev { transform: rotate(180deg); }
.team-pills { padding: 0 14px 12px; }
.team-pills .pills { margin: 0; }
.team-members { list-style: none; margin: 0; padding: 0; background: #f6f8fb; border-top: 1px solid var(--line); }
.team-members li { display: flex; align-items: center; gap: 10px; padding: 9px 14px; border-top: 1px solid var(--line); }
.team-members li:first-child { border-top: 0; }
.mrank { width: 22px; color: var(--muted); font-weight: 700; font-size: .9rem; font-variant-numeric: tabular-nums; }
.mname { font-weight: 600; }
.team-members b { font-size: 1.1rem; font-variant-numeric: tabular-nums; }
@media (max-width: 380px) { .team-pos { width: 22px; font-size: 1.3rem; } .team-badge { width: 40px; height: 40px; } }

/* ---------- return to TEFLSites ---------- */
.backbar { background: var(--ink); }
.backbar > * { margin-left: max(16px, calc((100% - 760px) / 2 + 16px)); }
.backbar { padding-top: 10px; }
.back-btn {
  display: inline-flex; align-items: center; gap: 6px; min-height: 40px; padding: 0 16px;
  border-radius: 99px; background: rgba(255,255,255,.14); border: 1px solid #4a6190;
  color: #fff; font-weight: 600; font-size: .9rem; text-decoration: none;
}
.back-btn:hover { background: rgba(255,255,255,.24); }
.login-box .back-btn { background: #eef2f9; border-color: var(--line); color: var(--ink); margin-bottom: 16px; }
.login-box .back-btn:hover { background: #e1e8f3; }