:root {
  --primary: #1d4ed8;
  --primary-dark: #1e3a8a;
  --primary-soft: #dbeafe;
  --accent: #f97316;
  --bg: #f1f5f9;
  --card: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --line: #e2e8f0;
  --ok: #16a34a;
  --danger: #dc2626;
  --gold: #f59e0b;
  --gold-soft: #fef3c7;
  --silver: #94a3b8;
  --silver-soft: #f1f5f9;
  --bronze: #c2410c;
  --bronze-soft: #ffedd5;
  --green-soft: #dcfce7;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(15, 23, 42, .08), 0 4px 16px rgba(15, 23, 42, .06);
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  min-height: 100vh; display: flex; flex-direction: column;
}
a { color: var(--primary); }
button, input, select { font: inherit; color: inherit; }
.container { width: 100%; max-width: 1280px; margin: 0 auto; padding: 16px; flex: 1; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.num { font-variant-numeric: tabular-nums; }

/* Header */
.site-header {
  background: linear-gradient(135deg, var(--primary-dark), var(--primary) 60%, #2563eb);
  color: #fff; box-shadow: var(--shadow);
}
.brand {
  max-width: 1280px; margin: 0 auto; padding: 12px 16px 8px;
  display: flex; align-items: center; justify-content: center; gap: 16px;
}
.logo { width: 72px; height: 72px; object-fit: contain; background: #fff; border-radius: 50%; padding: 4px; flex-shrink: 0; }
.brand h1 { margin: 0; text-align: center; font-size: 15px; font-weight: 700; letter-spacing: .3px; line-height: 1.35; }
.brand h1 span { font-size: 20px; color: #fde68a; font-weight: 800; }
.nav { max-width: 1280px; margin: 0 auto; padding: 0 12px 10px; display: flex; gap: 6px; justify-content: center; flex-wrap: wrap; }
.nav a, .nav button {
  color: #fff; text-decoration: none; background: rgba(255, 255, 255, .12); border: 0;
  padding: 6px 14px; border-radius: 999px; font-size: 14px; cursor: pointer;
}
.nav a.active { background: #fff; color: var(--primary); font-weight: 600; }
.nav .who { background: transparent; opacity: .85; cursor: default; padding: 6px 4px; }

/* Footer */
.site-footer { background: #0f172a; color: #cbd5e1; padding: 18px 16px 22px; text-align: center; font-size: 13px; }
.site-footer strong { color: #fff; font-size: 14px; display: block; margin-bottom: 6px; }
.site-footer ul { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; justify-content: center; gap: 4px 18px; }
.site-footer b { color: #fde68a; font-weight: 600; }
.site-footer a { color: #fde68a; }

/* Cards & controls */
.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; white-space: nowrap;
  border: 1px solid var(--line); background: #fff; padding: 8px 14px; border-radius: 8px; cursor: pointer; font-weight: 500;
}
.btn:hover { border-color: var(--primary); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-dark); }
.btn.danger { color: var(--danger); border-color: #fecaca; }
.btn.danger:hover { background: #fef2f2; }
.btn.sm { padding: 4px 10px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.input, select.input {
  width: 100%; padding: 9px 12px; border: 1px solid #cbd5e1; border-radius: 8px; background: #fff; min-height: 40px;
}
.input:focus { outline: 2px solid var(--primary-soft); border-color: var(--primary); }
.input.invalid { border-color: var(--danger); background: #fef2f2; }
label.field { display: block; font-size: 13px; color: var(--muted); font-weight: 500; }
label.field > .input, label.field > select { margin-top: 4px; color: var(--text); }
.err { color: var(--danger); font-size: 13px; min-height: 18px; margin-top: 4px; }

.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 12px; }
.toolbar .grow { flex: 1 1 220px; }
.toolbar select.input { width: auto; }

/* Tabs */
.tabs { display: flex; gap: 6px; margin-bottom: 12px; background: #e2e8f0; padding: 4px; border-radius: 12px; }
.tab {
  flex: 1; border: 0; background: transparent; padding: 10px 8px; border-radius: 9px; cursor: pointer; font-weight: 600; color: #334155;
  display: flex; flex-direction: column; align-items: center; line-height: 1.2;
}
.tab small { font-weight: 400; color: var(--muted); font-size: 12px; margin-top: 2px; }
.tab.active { background: #fff; color: var(--primary); box-shadow: var(--shadow); }

/* Live status */
.status { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; font-size: 13px; color: var(--muted); margin-bottom: 12px; }
.live { display: inline-flex; align-items: center; gap: 6px; font-weight: 600; color: var(--ok); }
.live::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: currentColor; animation: pulse 1.6s infinite; }
.live.off { color: var(--danger); }
.live.off::before { animation: none; }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: .25; } }
.progress { flex: 1 1 160px; height: 8px; background: #e2e8f0; border-radius: 99px; overflow: hidden; max-width: 260px; }
.progress > i { display: block; height: 100%; background: var(--ok); border-radius: 99px; transition: width .4s; }

/* Podium */
.podium { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-bottom: 14px; align-items: end; }
.podium .p { background: #fff; border-radius: var(--radius); box-shadow: var(--shadow); padding: 12px 10px; text-align: center; cursor: pointer; border-top: 5px solid; min-width: 0; }
.podium .p1 { order: 2; border-color: var(--gold); padding-top: 20px; }
.podium .p2 { order: 1; border-color: var(--silver); }
.podium .p3 { order: 3; border-color: var(--bronze); }
.podium .medal { font-size: 28px; line-height: 1; }
.podium .nm { font-weight: 700; margin-top: 6px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.podium .sc { font-size: 13px; color: var(--muted); }
.podium .sc b { color: var(--text); font-size: 16px; }
.podium .empty { color: var(--muted); font-style: italic; padding: 12px 0; }

/* Leaderboard table */
.table-wrap { overflow-x: auto; background: #fff; border-radius: var(--radius); box-shadow: var(--shadow); -webkit-overflow-scrolling: touch; }
table.board { width: 100%; border-collapse: separate; border-spacing: 0; font-size: 14px; }
.board th, .board td { padding: 9px 10px; border-bottom: 1px solid var(--line); text-align: center; white-space: nowrap; }
.board th { background: var(--primary-dark); color: #fff; font-weight: 600; font-size: 12.5px; position: sticky; top: 0; z-index: 2; }
.board th.grp { background: var(--primary); }
.board td.l, .board th.l { text-align: left; }
.board td.school { white-space: normal; min-width: 160px; color: #334155; }
.board td.total { font-weight: 800; color: var(--primary-dark); background: #eff6ff; }
.board td.ttime { font-weight: 700; background: #eff6ff; }
.board tbody tr:hover td { background: #f8fafc; }
.board tbody tr.top1 td { background: var(--gold-soft); }
.board tbody tr.top2 td { background: #f8fafc; }
.board tbody tr.top3 td { background: var(--bronze-soft); }
.board tbody tr.waiting td { color: var(--muted); }
.board tbody tr.flash td { animation: flash 2.2s ease-out; }
@keyframes flash { 0% { background: #fde047; } 100% { } }
.board .sticky1 { position: sticky; left: 0; z-index: 1; background: inherit; }
.board .sticky2 { position: sticky; left: 52px; z-index: 1; background: inherit; box-shadow: 2px 0 0 var(--line); }
.board tbody td.sticky1, .board tbody td.sticky2 { background: #fff; }
.board tbody tr.top1 td.sticky1, .board tbody tr.top1 td.sticky2 { background: var(--gold-soft); }
.board tbody tr.top3 td.sticky1, .board tbody tr.top3 td.sticky2 { background: var(--bronze-soft); }
.board th.sticky1, .board th.sticky2 { z-index: 3; background: var(--primary-dark); }
.board .sticky1 { min-width: 52px; }
.teamlink { background: none; border: 0; padding: 0; cursor: pointer; text-align: left; color: var(--primary); font-weight: 600; }
.teamlink:hover { text-decoration: underline; }
.teamlink small { display: block; color: var(--muted); font-weight: 400; font-size: 12px; }
.code { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size: 13px; }

.rank { display: inline-flex; align-items: center; justify-content: center; min-width: 30px; height: 30px; border-radius: 50%; font-weight: 800; background: #e2e8f0; padding: 0 6px; }
.rank.r1 { background: var(--gold); color: #fff; }
.rank.r2 { background: var(--silver); color: #fff; }
.rank.r3 { background: var(--bronze); color: #fff; }

.prize { display: inline-block; padding: 3px 10px; border-radius: 999px; font-size: 12.5px; font-weight: 700; }
.prize.nhat { background: var(--gold-soft); color: #92400e; border: 1px solid #fcd34d; }
.prize.nhi { background: #f1f5f9; color: #334155; border: 1px solid #cbd5e1; }
.prize.ba { background: var(--bronze-soft); color: #9a3412; border: 1px solid #fdba74; }
.prize.kk { background: var(--green-soft); color: #166534; border: 1px solid #86efac; }
.prize.tv { background: var(--primary-soft); color: var(--primary-dark); border: 1px solid #93c5fd; }
.yes { color: var(--ok); font-weight: 700; }

/* Modal */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(15, 23, 42, .55); z-index: 50;
  display: flex; align-items: flex-start; justify-content: center; padding: 24px 12px; overflow-y: auto;
}
.modal { background: #fff; border-radius: 16px; width: 100%; max-width: 560px; box-shadow: 0 20px 60px rgba(0, 0, 0, .3); position: relative; overflow: hidden; }
.modal.wide { max-width: 720px; }
.modal-close {
  position: absolute; top: 10px; right: 10px; width: 36px; height: 36px; border-radius: 50%; border: 0;
  background: rgba(255, 255, 255, .9); font-size: 22px; line-height: 1; cursor: pointer; z-index: 2;
}
.modal-body { padding: 18px; }
.modal h2 { margin: 0 0 12px; font-size: 18px; }

.hero { padding: 22px 18px 18px; color: #fff; text-align: center; background: linear-gradient(135deg, #334155, #475569); }
.hero.nhat { background: linear-gradient(135deg, #b45309, #f59e0b); }
.hero.nhi { background: linear-gradient(135deg, #475569, #94a3b8); }
.hero.ba { background: linear-gradient(135deg, #9a3412, #ea580c); }
.hero.kk { background: linear-gradient(135deg, #166534, #22c55e); }
.hero.tv { background: linear-gradient(135deg, var(--primary-dark), #3b82f6); }
.badge-rank {
  width: 96px; height: 96px; margin: 0 auto 8px; border-radius: 50%; background: rgba(255, 255, 255, .18);
  border: 4px solid rgba(255, 255, 255, .85); display: flex; flex-direction: column; align-items: center; justify-content: center;
}
.badge-rank small { font-size: 11px; letter-spacing: 1px; opacity: .9; }
.badge-rank b { font-size: 36px; line-height: 1; }
.hero .prize-title { font-size: 20px; font-weight: 800; text-transform: uppercase; letter-spacing: .5px; }
.hero .label { font-size: 11px; letter-spacing: 1px; opacity: .85; text-transform: uppercase; }
.hero .stats { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 14px; }
.hero .stat { background: rgba(255, 255, 255, .15); border-radius: 12px; padding: 10px; }
.hero .stat b { display: block; font-size: 26px; line-height: 1.15; }

.info { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; margin: 0 0 16px; font-size: 14px; }
.info dt { color: var(--muted); }
.info dd { margin: 0; font-weight: 600; }
table.detail { width: 100%; border-collapse: collapse; font-size: 14px; }
.detail th, .detail td { border: 1px solid var(--line); padding: 8px 10px; text-align: center; }
.detail th { background: var(--primary-soft); color: var(--primary-dark); }
.detail td:first-child { text-align: left; color: #334155; background: #f8fafc; font-weight: 500; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 16px; flex-wrap: wrap; }

/* Judge */
.team-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 10px; }
.team-card { background: #fff; border-radius: var(--radius); box-shadow: var(--shadow); padding: 12px; cursor: pointer; border: 2px solid transparent; text-align: left; width: 100%; }
.team-card:hover { border-color: var(--primary); }
.team-card .top { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
.team-card .nm { font-weight: 700; }
.team-card .sub { font-size: 12.5px; color: var(--muted); }
.chips { display: flex; gap: 6px; margin-top: 8px; }
.chip { flex: 1; font-size: 12.5px; padding: 4px 8px; border-radius: 8px; background: #f1f5f9; color: var(--muted); border: 1px dashed #cbd5e1; }
.chip.done { background: var(--green-soft); color: #166534; border: 1px solid #86efac; }
.round-box { border: 1px solid var(--line); border-radius: 12px; padding: 12px; margin-bottom: 12px; }
.round-box h3 { margin: 0 0 10px; font-size: 15px; display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.round-box .state { font-size: 12px; font-weight: 600; padding: 2px 8px; border-radius: 99px; }
.round-box .state.done { background: var(--green-soft); color: #166534; }
.round-box .state.none { background: #f1f5f9; color: var(--muted); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.kv { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; text-align: center; }
.kv div { background: #f8fafc; border-radius: 8px; padding: 6px; }
.kv small { display: block; color: var(--muted); font-size: 11.5px; }
.kv b { font-size: 17px; }
.meta { font-size: 12px; color: var(--muted); margin-top: 6px; }
.toggle { display: flex; gap: 0; margin-top: 4px; border: 1px solid #cbd5e1; border-radius: 8px; overflow: hidden; }
.toggle button { flex: 1; border: 0; background: #fff; padding: 8px; cursor: pointer; min-height: 40px; }
.toggle button.on { background: var(--primary); color: #fff; font-weight: 600; }

/* Login */
.login { max-width: 380px; margin: 24px auto; }
.login h2 { margin-top: 0; }
.login .field { margin-bottom: 12px; }

/* Admin */
.subtabs { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 12px; }
.subtabs button { border: 1px solid var(--line); background: #fff; padding: 7px 14px; border-radius: 999px; cursor: pointer; }
.subtabs button.active { background: var(--primary); border-color: var(--primary); color: #fff; }
table.simple { width: 100%; border-collapse: collapse; font-size: 14px; }
.simple th, .simple td { padding: 8px 10px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; }
.simple th { background: #f8fafc; font-size: 12.5px; color: var(--muted); position: sticky; top: 0; }
.users-grid { display: grid; gap: 10px; }
.user-row { display: grid; grid-template-columns: 140px 1fr 1fr auto; gap: 8px; align-items: end; }
.audit td { font-size: 13px; }
.audit td.d { white-space: normal; color: #334155; }

.toast {
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%) translateY(20px); background: #0f172a; color: #fff;
  padding: 10px 18px; border-radius: 10px; opacity: 0; pointer-events: none; transition: .25s; z-index: 100; max-width: 90vw; text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%); }
.toast.err { background: var(--danger); }
.empty-state { text-align: center; padding: 30px; color: var(--muted); }

.sm-only { display: none; }
@media (max-width: 720px) {
  .hide-sm { display: none; }
  .sm-only { display: inline; }
  .podium .sc + .sc { display: none; }
  .container { padding: 12px; }
  .brand { gap: 10px; padding: 10px 12px 6px; }
  .logo { width: 48px; height: 48px; padding: 2px; }
  .brand h1 { font-size: 11.5px; }
  .brand h1 span { font-size: 14px; }
  .podium { gap: 6px; }
  .podium .p { padding: 10px 6px; }
  .podium .nm { font-size: 13px; }
  .board th, .board td { padding: 8px 7px; font-size: 13px; }
  .board td.school { min-width: 130px; }
  .board .sticky2 { left: 44px; }
  .board .sticky1 { min-width: 44px; }
  .board td.sticky2 { min-width: 150px; max-width: 170px; white-space: normal; }
  .user-row { grid-template-columns: 1fr 1fr; }
  .user-row > :first-child { grid-column: 1 / -1; }
  .kv { grid-template-columns: 1fr 1fr; }
  .hero .stat b { font-size: 21px; }
}
@media print {
  .site-header .nav, .toolbar, .tabs, .podium, .status { display: none !important; }
  .table-wrap { box-shadow: none; overflow: visible; }
}
