:root {
  --bg: #f6f5f0;
  --surface: #ffffff;
  --surface-2: #efeee8;
  --text: #1b1d1a;
  --muted: #676b63;
  --line: #dddbd2;
  --accent: #0f6b3a;
  --accent-ink: #ffffff;
  --accent-soft: #dcefe3;
  --mine: #1f5fd1;
  --mine-soft: #dce7fb;
  --win: #f5c542;
  --win-ink: #3d2e00;
  --danger: #b42318;
  --taken: #c9c7bd;
  --radius: 14px;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.05), 0 4px 16px rgb(0 0 0 / 0.04);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    --bg: #121412;
    --surface: #1b1e1b;
    --surface-2: #262a26;
    --text: #eceee9;
    --muted: #9da298;
    --line: #333832;
    --accent: #3dbb73;
    --accent-ink: #0b1f12;
    --accent-soft: #1d3a28;
    --mine: #79a6ff;
    --mine-soft: #1f2d4a;
    --taken: #4a4f48;
    --danger: #ff7b6e;
    --shadow: none;
    color-scheme: dark;
  }
}
:root[data-theme='dark'] {
  --bg: #121412;
  --surface: #1b1e1b;
  --surface-2: #262a26;
  --text: #eceee9;
  --muted: #9da298;
  --line: #333832;
  --accent: #3dbb73;
  --accent-ink: #0b1f12;
  --accent-soft: #1d3a28;
  --mine: #79a6ff;
  --mine-soft: #1f2d4a;
  --taken: #4a4f48;
  --danger: #ff7b6e;
  --shadow: none;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  padding-bottom: env(safe-area-inset-bottom);
}
h1, h2, h3 { line-height: 1.2; margin: 0 0 8px; }
h1 { font-size: 1.6rem; letter-spacing: -0.01em; }
h2 { font-size: 1.1rem; }
h3 { font-size: 1rem; }
p { margin: 0 0 10px; }
a { color: var(--accent); }
button, input, select, textarea { font: inherit; color: inherit; }

.loading { padding: 40vh 16px; text-align: center; color: var(--muted); }
.wrap { max-width: 560px; margin: 0 auto; padding: 8px 16px 40px; }
.wrap.narrow { max-width: 420px; }
.wrap.has-bar { padding-bottom: 110px; }
.stack > * + * { margin-top: 14px; }
.muted { color: var(--muted); }
.block { display: block; }
.center { text-align: center; }
.hint { color: var(--muted); font-size: 0.9rem; }
.fine { color: var(--muted); font-size: 0.78rem; text-align: center; margin-top: 24px; }
.mode-note { max-width: 560px; margin: 0 auto 24px; padding: 0 16px; color: var(--muted); font-size: 0.78rem; text-align: center; }
.page-title { margin: 8px 0 14px; }
.section-title { margin: 22px 2px 4px; }

/* top bar */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 800; color: var(--text); text-decoration: none; }
.logo {
  width: 22px; height: 22px; border-radius: 6px; background: var(--accent);
  background-image: linear-gradient(var(--bg) 0 0), linear-gradient(var(--bg) 0 0);
  background-size: 2px 100%, 100% 2px; background-position: center; background-repeat: no-repeat;
}
.toplink { font-size: 0.9rem; font-weight: 600; text-decoration: none; }

/* cards & lists */
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 16px; margin-top: 14px; box-shadow: var(--shadow);
}
.hero { padding: 24px 2px 6px; }
.hero h1 { font-size: 2rem; }
.hero p { color: var(--muted); }
.list { list-style: none; margin: 0; padding: 0; }
.list li + li { border-top: 1px solid var(--line); }
.list a { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 12px 2px; color: var(--text); text-decoration: none; }
.list small { color: var(--muted); white-space: nowrap; }

/* buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 0 16px; border-radius: 11px;
  border: 1px solid var(--line); background: var(--surface-2); color: var(--text);
  font-weight: 650; text-decoration: none; cursor: pointer;
}
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.btn.ghost { background: transparent; }
.btn.block { display: flex; width: 100%; }
.btn.sm { min-height: 36px; padding: 0 12px; font-size: 0.9rem; }
.btn:active { transform: translateY(1px); }
.btn:focus-visible, .cell:focus-visible, .chip:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--accent) 55%, transparent); outline-offset: 2px;
}
.icon-btn { width: 40px; height: 40px; flex: none; border: 0; background: none; color: var(--muted); font-size: 1.4rem; cursor: pointer; border-radius: 10px; }
.row { display: flex; gap: 10px; align-items: center; }
.row.between { justify-content: space-between; }
.grow { flex: 1; }

/* forms */
.lbl { display: block; font-weight: 600; font-size: 0.9rem; margin: 12px 0 0; }
.lbl > input, .lbl > select, .lbl > textarea, .lbl > .prefix { margin-top: 6px; }
.opt { font-weight: 400; color: var(--muted); }
input, select, textarea {
  width: 100%; min-height: 46px; padding: 10px 12px;
  border: 1px solid var(--line); border-radius: 10px; background: var(--bg);
  font-size: 16px; /* prevents iOS zoom */
}
textarea { resize: vertical; }
.row input { flex: 1; }
.two { display: grid; gap: 4px 14px; grid-template-columns: 1fr; }
@media (min-width: 480px) { .two { grid-template-columns: 1fr 1fr; } }
.prefix, .suffix { display: flex; align-items: center; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); overflow: hidden; }
.prefix span, .suffix span { padding: 0 10px; color: var(--muted); }
.prefix input, .suffix input { border: 0; margin: 0; min-width: 0; }
.form-error { color: var(--danger); font-weight: 600; min-height: 1em; margin: 8px 0 0; }
fieldset { border: 0; padding: 0; margin: 0; min-width: 0; }
.seg { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.seg legend { margin-bottom: 6px; }
.seg label { position: relative; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg span { display: block; height: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; font-weight: 650; cursor: pointer; }
.seg small { display: block; font-weight: 400; color: var(--muted); font-size: 0.8rem; }
.seg input:checked + span { border-color: var(--accent); background: var(--accent-soft); box-shadow: inset 0 0 0 1px var(--accent); }
.seg input:focus-visible + span { outline: 3px solid color-mix(in srgb, var(--accent) 55%, transparent); }
.seg:disabled { opacity: 0.6; }
.swatches { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.swatch { position: relative; }
.swatch input { position: absolute; opacity: 0; }
.swatch span { display: block; width: 28px; height: 28px; border-radius: 50%; background: var(--c); cursor: pointer; border: 2px solid var(--surface); box-shadow: 0 0 0 1px var(--line); }
.swatch input:checked + span { box-shadow: 0 0 0 2px var(--text); }
.swatch input:focus-visible + span { outline: 3px solid color-mix(in srgb, var(--accent) 55%, transparent); }
.period-row { display: flex; gap: 8px; align-items: center; margin-top: 8px; }
.period-row > input { flex: 1; min-width: 0; }
.period-row .suffix { width: 96px; flex: none; }
.pct-total { font-weight: 700; color: var(--accent); }
.pct-total.bad { color: var(--danger); }

/* pool header */
.pool-head { padding: 10px 2px 0; }
.pool-head h1 { margin-top: 8px; }
.pill { display: inline-block; padding: 3px 10px; border-radius: 99px; font-size: 0.78rem; font-weight: 700; background: var(--surface-2); }
.pill.open { background: var(--accent-soft); color: var(--accent); }
.pill.live { background: var(--win); color: var(--win-ink); }
.pill.admin { background: var(--mine-soft); color: var(--mine); }
.stats { display: flex; gap: 8px; margin-top: 10px; }
.stats > div { flex: 1; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 8px 10px; }
.card .stats { margin: 0; }
.card .stats > div { background: var(--surface-2); border: 0; }
.stats b { display: block; font-size: 1.1rem; font-variant-numeric: tabular-nums; }
.stats small { color: var(--muted); font-size: 0.78rem; }

/* period chips */
.chips { display: flex; gap: 6px; overflow-x: auto; padding: 4px 2px 8px; scrollbar-width: none; }
.chip { flex: none; min-height: 36px; padding: 0 14px; border-radius: 99px; border: 1px solid var(--line); background: var(--surface); font-weight: 600; font-size: 0.88rem; cursor: pointer; }
.chip.on { background: var(--text); color: var(--bg); border-color: var(--text); }
.chip.won::after { content: ' 🏆'; }

/* board */
.board {
  display: grid; grid-template-columns: 22px 1fr; grid-template-rows: auto 1fr; gap: 4px;
  margin-top: 6px;
}
.team-top {
  grid-column: 2; background: var(--ca); color: var(--ia); text-align: center;
  display: flex; align-items: center; justify-content: center; gap: 6px; min-width: 0;
  font-weight: 800; font-size: 0.85rem; letter-spacing: 0.06em; text-transform: uppercase;
  border-radius: 8px; padding: 4px 6px;
}
.team-top > span:last-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.team-side {
  grid-row: 2; background: var(--cb); color: var(--ib); border-radius: 8px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px; overflow: hidden; padding: 3px 0;
}
.team-side > span:last-child {
  writing-mode: vertical-rl; transform: rotate(180deg);
  font-weight: 800; font-size: 0.85rem; letter-spacing: 0.06em; text-transform: uppercase; white-space: nowrap;
}
.grid { display: grid; grid-template-columns: repeat(calc(var(--n) + 1), minmax(0, 1fr)); gap: 2px; }
.hd {
  display: flex; align-items: center; justify-content: center; aspect-ratio: 1;
  font-weight: 800; font-variant-numeric: tabular-nums; font-size: clamp(10px, 3vw, 15px); border-radius: 5px;
}
.n5 .hd { font-size: clamp(12px, 3.6vw, 18px); }
.hd.col { background: color-mix(in srgb, var(--ca) 16%, var(--surface)); color: var(--text); box-shadow: inset 0 -3px 0 var(--ca); }
.hd.rowh { background: color-mix(in srgb, var(--cb) 16%, var(--surface)); color: var(--text); box-shadow: inset -3px 0 0 var(--cb); }
.cell {
  aspect-ratio: 1; min-width: 0; padding: 0; border: 1px solid var(--line); border-radius: 5px;
  background: var(--surface); font-weight: 700; font-size: clamp(9px, 2.6vw, 13px); cursor: pointer;
  display: flex; align-items: center; justify-content: center; position: relative; touch-action: manipulation;
}
.n5 .cell { font-size: clamp(12px, 3.6vw, 17px); border-radius: 8px; }
.cell:disabled { cursor: default; }
.cell.taken { background: var(--taken); border-color: transparent; color: var(--text); }
.cell.hidden::before { content: ''; width: 30%; height: 30%; border-radius: 50%; background: color-mix(in srgb, var(--text) 30%, transparent); }
.cell.mine { background: var(--mine); color: #fff; }
.cell.mine.hidden::before { background: rgb(255 255 255 / 0.7); }
.cell.sel { background: var(--accent-soft); border: 2px solid var(--accent); color: var(--accent); }
.cell.unpaid { box-shadow: inset 0 0 0 2px var(--danger); }
.cell.win { background: var(--win); color: var(--win-ink); border-color: transparent; z-index: 1; box-shadow: 0 0 0 2px var(--win-ink); }
.legend { display: flex; flex-wrap: wrap; justify-content: center; gap: 6px 14px; margin: 10px 0 4px; font-size: 0.8rem; color: var(--muted); }
.legend span { display: inline-flex; align-items: center; gap: 5px; }
.lg { width: 12px; height: 12px; border-radius: 3px; display: inline-block; border: 1px solid var(--line); background: var(--surface); }
.lg.taken { background: var(--taken); border-color: transparent; }
.lg.mine { background: var(--mine); border-color: transparent; }
.lg.win { background: var(--win); border-color: transparent; }
.lg.unpaid { box-shadow: inset 0 0 0 2px var(--danger); }

/* prizes & results */
.prizes { list-style: none; padding: 0; margin: 0; }
.prizes li { padding: 10px 0; }
.prizes li + li { border-top: 1px solid var(--line); }
.p-head { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; }
.p-head span { font-variant-numeric: tabular-nums; color: var(--muted); font-size: 0.9rem; white-space: nowrap; }
.p-head em { font-style: normal; color: var(--accent); }
.p-res { margin-top: 4px; display: flex; flex-direction: column; }
.p-res small { color: var(--muted); }
.winner { font-weight: 700; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; margin: 0; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; overflow-wrap: anywhere; }
.rules { margin: 0; padding-left: 20px; }
.rules li + li { margin-top: 6px; }
.mine-card { border-color: var(--mine); }

/* organizer */
.tabs { display: flex; gap: 4px; margin-top: 14px; padding: 4px; background: var(--surface-2); border-radius: 12px; }
.tabs button { flex: 1; min-height: 40px; border: 0; border-radius: 9px; background: none; font-weight: 650; cursor: pointer; color: var(--muted); }
.tabs button.on { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.draw { display: grid; gap: 6px; margin: 4px 0 10px; }
.draw div { display: flex; gap: 10px; align-items: baseline; }
.draw small { width: 84px; flex: none; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.draw span { font-weight: 800; font-variant-numeric: tabular-nums; letter-spacing: 0.08em; overflow-wrap: anywhere; }
.period-card .p-head { margin-bottom: 8px; }
.period-card h3 { margin: 0; }
.score-form { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 10px 0 6px; align-items: end; }
.score-form label { display: flex; flex-direction: column; min-width: 0; font-size: 0.82rem; font-weight: 600; color: var(--muted); }
.score-form label span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.score-form input { margin-top: 4px; font-weight: 700; color: var(--text); }
.score-form .btn { grid-column: span 1; }
.player .btn { flex: none; }
.player small.block { margin-top: 2px; }
.sq-chips { display: flex; flex-wrap: wrap; gap: 6px; }
.sq-chip { display: inline-flex; border: 1px solid var(--line); border-radius: 99px; overflow: hidden; font-size: 0.82rem; }
.sq-chip button { border: 0; background: none; min-height: 34px; padding: 0 10px; cursor: pointer; font-weight: 600; }
.sq-chip button + button { padding: 0 10px 0 4px; color: var(--muted); }
.sq-chip.paid { background: var(--accent-soft); border-color: var(--accent); }
.sq-chip.paid button:first-child { color: var(--accent); }
.code { font-size: 2rem; font-weight: 800; letter-spacing: 0.2em; text-align: center; padding: 10px; margin: 6px 0 10px; background: var(--surface-2); border-radius: 12px; }
.linkbox { margin-bottom: 10px; font-size: 14px; color: var(--muted); }

/* selection bar & sheet */
.selbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: flex; gap: 8px; align-items: center;
  padding: 12px max(16px, calc((100vw - 560px) / 2 + 16px)) calc(12px + env(safe-area-inset-bottom));
  background: var(--surface); border-top: 1px solid var(--line); box-shadow: 0 -6px 24px rgb(0 0 0 / 0.08);
}
.selbar > div { flex: 1; min-width: 0; }
.selbar small { display: block; color: var(--muted); }
.backdrop { position: fixed; inset: 0; background: rgb(0 0 0 / 0.4); z-index: 20; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 21;
  max-width: 560px; margin: 0 auto; background: var(--surface);
  border-radius: 20px 20px 0 0; padding: 20px 16px calc(16px + env(safe-area-inset-bottom));
  animation: up 0.18s ease-out;
}
.sheet .row { margin-top: 16px; }
@keyframes up { from { transform: translateY(30px); opacity: 0; } }
@media (prefers-reduced-motion: reduce) { .sheet { animation: none; } }

.toast {
  position: fixed; left: 50%; bottom: calc(96px + env(safe-area-inset-bottom)); z-index: 30;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  max-width: calc(100vw - 32px); padding: 10px 16px; border-radius: 12px;
  background: var(--text); color: var(--bg); font-weight: 600; font-size: 0.92rem; text-align: center;
  transition: opacity 0.18s, transform 0.18s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* custom color swatch: rainbow until a color is picked */
.swatch.custom span { background: conic-gradient(#e33, #fd0, #3c5, #2bd, #36f, #c3f, #e33); }
.swatch.custom.set span { background: var(--c); box-shadow: 0 0 0 1px var(--line), inset 0 0 0 2px rgb(255 255 255 / 0.55); }
.swatch.custom.set input:checked + span { box-shadow: 0 0 0 2px var(--text), inset 0 0 0 2px rgb(255 255 255 / 0.55); }
.swatch input[type='color'] {
  position: absolute; inset: 0; width: 100%; height: 100%; min-height: 0; padding: 0; margin: 0;
  opacity: 0; cursor: pointer; border: 0;
}

/* team logos */
.avatar {
  flex: none; width: 18px; height: 18px; border-radius: 50%; overflow: hidden;
  background: #fff; display: inline-flex; align-items: center; justify-content: center;
}
.avatar img { width: 100%; height: 100%; object-fit: contain; display: block; }
.avatar.lg { width: 44px; height: 44px; border: 1px solid var(--line); }
.avatar.lg.empty { background: var(--surface-2); color: var(--muted); font-size: 1.3rem; }
.logo-field { display: flex; align-items: center; gap: 8px; margin-top: 10px; }
.logo-field label.btn { position: relative; }
.logo-field label.btn:focus-within { outline: 3px solid color-mix(in srgb, var(--accent) 55%, transparent); outline-offset: 2px; }
.visually-hidden { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); border: 0; min-height: 0; }
.team-fields + .team-fields { margin-top: 10px; }
@media (min-width: 480px) { .team-fields + .team-fields { margin-top: 0; } }
.swatch input[type='radio'] { width: 0; height: 0; min-height: 0; padding: 0; margin: 0; }
.team-top { padding: 3px 6px; }
.team-top .avatar { width: 22px; height: 22px; }
