:root {
  color-scheme: light;
  --bg: #f4f4f1;
  --surface: #ffffff;
  --surface-2: #f0efea;
  --border: #e0ded6;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #77756e;
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --ok: #16794a;
  --ok-bg: #e5f4ec;
  --err: #b3362a;
  --err-bg: #fbe9e6;
  --warn: #8a5a00;
  --warn-bg: #fdf1d8;
  --info-bg: #e6eefb;
  --series-1: #2a78d6; --series-2: #eb6834; --series-3: #1baf7a; --series-4: #eda100;
  --series-5: #e87ba4; --series-6: #008300; --series-7: #6250d6; --series-8: #e34948;
  --radius: 10px;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121211; --surface: #1a1a19; --surface-2: #232321; --border: #33322f;
    --text: #f0efec; --text-2: #c3c2b7; --muted: #95938a;
    --accent: #3987e5; --accent-ink: #ffffff;
    --ok: #4cc28b; --ok-bg: #16291f; --err: #ef7d73; --err-bg: #331a17;
    --warn: #e7b14a; --warn-bg: #2f2512; --info-bg: #182436;
    --series-1: #3987e5; --series-2: #d95926; --series-3: #199e70; --series-4: #c98500;
    --series-5: #d55181; --series-6: #008300; --series-7: #9085e9; --series-8: #e66767;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121211; --surface: #1a1a19; --surface-2: #232321; --border: #33322f;
  --text: #f0efec; --text-2: #c3c2b7; --muted: #95938a;
  --accent: #3987e5; --accent-ink: #ffffff;
  --ok: #4cc28b; --ok-bg: #16291f; --err: #ef7d73; --err-bg: #331a17;
  --warn: #e7b14a; --warn-bg: #2f2512; --info-bg: #182436;
  --series-1: #3987e5; --series-2: #d95926; --series-3: #199e70; --series-4: #c98500;
  --series-5: #d55181; --series-6: #008300; --series-7: #9085e9; --series-8: #e66767;
}

* { 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, sans-serif;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
code, .mono { font-family: var(--mono); font-size: 0.92em; }
h1 { font-size: 1.5rem; margin: 0; letter-spacing: -0.01em; }
h2, h3 { margin: 0; }
.wrap { max-width: 1120px; margin: 0 auto; padding: 0 16px; }
.main { padding-top: 24px; padding-bottom: 48px; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.center { text-align: center; }
.nowrap { white-space: nowrap; }
.strong { font-weight: 600; }

/* top bar */
.topbar { background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 10; }
.topbar-in { display: flex; align-items: center; gap: 20px; min-height: 56px; flex-wrap: wrap; }
.brand { font-weight: 700; color: var(--text); font-size: 1.05rem; white-space: nowrap; }
.brand:hover { text-decoration: none; }
.brand-mark { color: var(--accent); }
.nav { display: flex; gap: 4px; flex: 1; overflow-x: auto; scrollbar-width: none; }
.nav a { color: var(--text-2); padding: 6px 10px; border-radius: 6px; white-space: nowrap; }
.nav a:hover { background: var(--surface-2); text-decoration: none; }
.nav a.on { color: var(--text); background: var(--surface-2); font-weight: 600; }
.who { display: flex; align-items: center; gap: 12px; }
.who-name { font-weight: 600; max-width: 160px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.who form { margin: 0; }
@media (max-width: 640px) {
  .topbar-in { gap: 8px 12px; padding-top: 8px; padding-bottom: 4px; }
  .nav { order: 3; flex-basis: 100%; }
  .who { margin-left: auto; }
}

.contest-bar { font-size: 0.9rem; padding: 8px 0; border-bottom: 1px solid var(--border); }
.contest-bar.state-not_started, .contest-bar.state-paused { background: var(--warn-bg); color: var(--warn); }
.contest-bar.state-ended { background: var(--surface-2); color: var(--text-2); }
.contest-bar.state-running { background: var(--info-bg); color: var(--text-2); }

/* buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  font: inherit; font-weight: 500; padding: 8px 14px; border-radius: 8px; cursor: pointer;
  background: var(--surface); color: var(--text); border: 1px solid var(--border); white-space: nowrap;
}
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn-primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn-primary:hover { background: var(--accent); filter: brightness(1.08); }
.btn-danger { color: var(--err); border-color: var(--err); }
.btn-sm { padding: 5px 10px; font-size: 0.9rem; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-link { background: none; border: 0; padding: 0; font: inherit; color: var(--accent); cursor: pointer; }
.btn-link:hover { text-decoration: underline; }
.btn-link.danger { color: var(--err); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* forms */
input, textarea, select {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; width: 100%;
}
input:focus, textarea:focus, select:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent); }
input[readonly] { background: var(--surface-2); color: var(--text-2); }
label { display: flex; flex-direction: column; gap: 6px; font-weight: 500; font-size: 0.92rem; }
label .muted { font-weight: 400; }
label.check { flex-direction: row; align-items: center; gap: 8px; font-weight: 400; }
label.check input { width: auto; }
.stack { display: flex; flex-direction: column; gap: 14px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.form-grid .span-2 { grid-column: span 2; }
@media (max-width: 640px) { .form-grid { grid-template-columns: 1fr; } .form-grid .span-2 { grid-column: auto; } }
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.actions form { margin: 0; }
.code-area { font-family: var(--mono); font-size: 0.88rem; line-height: 1.6; white-space: pre; overflow-x: auto; }
.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 16px; }
.filters select { width: auto; min-width: 160px; }

/* cards & layout */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; margin-bottom: 16px; }
.card.narrow { max-width: 420px; margin: 40px auto; }
.card-title { font-size: 1rem; margin-bottom: 12px; display: flex; align-items: baseline; gap: 8px; justify-content: space-between; }
.card-title .muted { font-weight: 400; margin-right: auto; }
.page-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.page-head .actions { margin-left: auto; }
.cols { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; align-items: start; }
@media (max-width: 800px) { .cols { grid-template-columns: 1fr; } }

/* alerts, tags */
.alert { border-radius: 8px; padding: 10px 14px; margin-bottom: 16px; border: 1px solid transparent; }
.alert ul { margin: 6px 0; padding-left: 20px; }
.alert-success { background: var(--ok-bg); color: var(--ok); border-color: color-mix(in srgb, var(--ok) 30%, transparent); }
.alert-error { background: var(--err-bg); color: var(--err); border-color: color-mix(in srgb, var(--err) 30%, transparent); }
.alert-info { background: var(--info-bg); color: var(--text); border-color: var(--border); }
.alert-sm { padding: 6px 10px; margin: 8px 0 0; font-size: 0.9rem; }
.badge { font-size: 0.8rem; padding: 3px 9px; border-radius: 999px; background: var(--surface-2); color: var(--text-2); border: 1px solid var(--border); }
.badge-warn, .badge.state-paused, .badge.state-not_started { background: var(--warn-bg); color: var(--warn); border-color: transparent; }
.badge.state-running { background: var(--ok-bg); color: var(--ok); border-color: transparent; }
.tag { font-size: 0.8rem; padding: 1px 7px; border-radius: 5px; background: var(--surface-2); color: var(--text-2); white-space: nowrap; }
.tag-ok { background: var(--ok-bg); color: var(--ok); }
.tag-err { background: var(--err-bg); color: var(--err); }

/* stats */
.team-summary, .tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin-bottom: 24px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; display: flex; flex-direction: column; gap: 2px; }
.stat-label { color: var(--muted); font-size: 0.85rem; }
.stat-value { font-size: 1.7rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat-value small { font-size: 0.95rem; color: var(--muted); font-weight: 500; }

/* challenges */
.cat { margin-bottom: 28px; }
.cat-title { font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.08em; color: var(--text-2); margin-bottom: 10px; }
.cat-title .muted { letter-spacing: 0; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 12px; }
.chal { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; display: flex; flex-direction: column; gap: 8px; scroll-margin-top: 90px; }
.chal.is-solved { border-color: color-mix(in srgb, var(--ok) 45%, var(--border)); background: color-mix(in srgb, var(--ok-bg) 55%, var(--surface)); }
.chal-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
.chal-head h3 { font-size: 1.02rem; }
.pts { font-family: var(--mono); font-weight: 700; font-size: 0.95rem; background: var(--surface-2); padding: 2px 8px; border-radius: 6px; white-space: nowrap; }
.chal-desc { margin: 0; color: var(--text-2); white-space: pre-line; overflow-wrap: anywhere; }
.chal-meta { margin: 0; font-size: 0.85rem; }
.flag-form { display: flex; gap: 8px; margin-top: auto; }
.flag-form input { font-family: var(--mono); min-width: 0; }
.solved-tag { margin: auto 0 0; color: var(--ok); font-weight: 600; font-size: 0.92rem; }
.solved-tag .muted { font-weight: 400; font-size: 0.85rem; }

/* tables */
.table-scroll { overflow-x: auto; margin: -4px; padding: 4px; }
.table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.table th { text-align: left; font-size: 0.8rem; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; padding: 8px 10px; border-bottom: 1px solid var(--border); white-space: nowrap; }
.table td { padding: 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table tr:last-child td { border-bottom: 0; }
.table .num { text-align: right; width: 1%; white-space: nowrap; }
.table tr.is-me td { background: var(--info-bg); }
.table tr.is-off td { color: var(--muted); }
.rank { font-weight: 700; color: var(--text-2); }
.rank-1, .rank-2, .rank-3 { color: var(--text); }
.rank-1::before { content: "🥇 "; } .rank-2::before { content: "🥈 "; } .rank-3::before { content: "🥉 "; }
.row-actions { display: flex; gap: 12px; justify-content: flex-end; white-space: nowrap; }
.row-actions form { margin: 0; }
details.secret summary { cursor: pointer; color: var(--accent); font-size: 0.88rem; }
details.secret code { display: block; margin-top: 4px; word-break: break-all; }
@media (max-width: 640px) { .hide-sm { display: none; } }

/* chart */
.chart { position: relative; width: 100%; }
.chart-svg { display: block; overflow: visible; }
.chart-svg .grid { stroke: var(--border); stroke-width: 1; }
.chart-svg .axis { stroke: var(--muted); stroke-width: 1; }
.chart-svg .tick { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart-svg .line { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart-svg .direct-label { fill: var(--text-2); font-size: 12px; }
.chart-svg .crosshair { stroke: var(--muted); stroke-width: 1; stroke-dasharray: 3 3; }
.chart-svg .dot { stroke: var(--surface); stroke-width: 2; }
.chart-tip {
  position: absolute; pointer-events: none; background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: 8px 10px; font-size: 0.82rem; box-shadow: 0 4px 16px rgb(0 0 0 / 0.12); min-width: 160px; z-index: 2;
}
.tip-time { color: var(--muted); margin-bottom: 4px; }
.tip-row { display: flex; align-items: center; gap: 8px; }
.tip-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 160px; }
.tip-val { font-weight: 600; font-variant-numeric: tabular-nums; }
.legend { list-style: none; padding: 0; margin: 12px 0 0; display: flex; flex-wrap: wrap; gap: 6px 16px; font-size: 0.85rem; color: var(--text-2); }
.legend li { display: flex; align-items: center; gap: 6px; }
.swatch { width: 10px; height: 10px; border-radius: 3px; flex: none; display: inline-block; }
