:root {
  --bg: #f4efe4;
  --card: #fffcf5;
  --ink: #1e2320;
  --muted: #74776f;
  --line: #e6dcc8;
  --felt: #1f5240;
  --felt-ink: #f6f1e6;
  --win: #c4362b;
  --win-bg: #fbe8e3;
  --lose: #237556;
  --lose-bg: #e2f1e8;
  --gold: #c9962f;
  --warn: #b7741a; /* warnings, shortfalls: never red/green, those mean win/loss */
  --radius: 14px;
  --shadow: 0 1px 2px rgb(60 40 10 / 6%), 0 6px 18px rgb(60 40 10 / 5%);
  --tabbar-h: 60px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101915;
    --card: #17231e;
    --ink: #ece6d8;
    --muted: #9aa39b;
    --line: #27382f;
    --felt: #143729;
    --win: #ff7a6a;
    --win-bg: #3b201c;
    --lose: #5cc99c;
    --lose-bg: #16322a;
    --warn: #e2a64e;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

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

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans SC", sans-serif;
  -webkit-tap-highlight-color: transparent;
}

button, input, select, textarea { font: inherit; color: inherit; }
a { color: inherit; }
[hidden] { display: none !important; }

.num { font-variant-numeric: tabular-nums; }
.win { color: var(--win); }
.lose { color: var(--lose); }
.zero, .muted { color: var(--muted); }
.warn { color: var(--warn); }
.ok { color: var(--felt); font-weight: 600; }
@media (prefers-color-scheme: dark) { .ok { color: var(--ink); } }
.small { font-size: 13px; }

/* ---- frame ---------------------------------------------------------------- */

.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
  background: var(--felt); color: var(--felt-ink);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 18px; letter-spacing: .08em; }
.whoami {
  max-width: 45%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  padding: 4px 10px; border-radius: 999px; background: rgb(255 255 255 / 12%);
  font-size: 14px; text-decoration: none;
}

.tile {
  display: inline-grid; place-items: center;
  width: 26px; height: 34px; border-radius: 6px;
  background: #fffcf5; color: #c4362b;
  font-weight: 800; font-size: 18px; letter-spacing: 0;
  box-shadow: inset 0 -3px 0 #d8cfbb, 0 1px 2px rgb(0 0 0 / 25%);
}
.tile-lg { width: 52px; height: 68px; border-radius: 10px; font-size: 36px; box-shadow: inset 0 -5px 0 #d8cfbb, 0 2px 6px rgb(0 0 0 / 20%); }

.view {
  max-width: 640px; margin: 0 auto;
  padding: 16px 16px calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 24px);
}

.tabbar {
  position: fixed; inset: auto 0 0 0; z-index: 20;
  display: grid; grid-auto-flow: column; grid-auto-columns: 1fr;
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--card); border-top: 1px solid var(--line);
}
.tabbar a {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  font-size: 12px; text-decoration: none; color: var(--muted);
}
.tabbar svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }
.tabbar a.active { color: var(--felt); font-weight: 600; }
@media (prefers-color-scheme: dark) { .tabbar a.active { color: var(--lose); } }

/* ---- building blocks ------------------------------------------------------ */

.card {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 14px 16px; margin-bottom: 12px;
}
.card-title { margin: 0 0 10px; font-size: 14px; font-weight: 600; color: var(--muted); letter-spacing: .04em; }
.page-title { margin: 4px 0 14px; font-size: 22px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 40px; padding: 0 14px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--card); cursor: pointer; text-decoration: none;
}
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--felt); color: var(--felt-ink); border-color: transparent; font-weight: 600; }
.btn-danger { color: var(--win); border-color: currentColor; background: transparent; }
.btn-block { width: 100%; min-height: 48px; }
.btn-sm { min-height: 32px; padding: 0 10px; font-size: 14px; border-radius: 8px; }
.btn:disabled { opacity: .5; cursor: default; }

.field {
  width: 100%; min-height: 44px; padding: 8px 12px;
  border: 1px solid var(--line); border-radius: 10px; background: var(--bg); color: var(--ink);
  font-size: 16px; /* >= 16px stops iOS from zooming on focus */
}
.field:focus, .btn:focus-visible { outline: 2px solid var(--gold); outline-offset: 1px; }

.loading, .empty { padding: 48px 0; text-align: center; color: var(--muted); }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 16px); z-index: 50;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  max-width: calc(100% - 32px); padding: 10px 16px; border-radius: 10px;
  background: #222; color: #fff; font-size: 14px; transition: .2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: var(--win); }

/* Admin-only controls disappear for members. */
body:not(.is-admin) [data-admin] { display: none !important; }

.gate { position: fixed; inset: 0; z-index: 100; display: grid; place-items: center; padding: 16px; background: var(--felt); overflow-y: auto; }
.gate-card { width: min(360px, 100%); display: grid; gap: 12px; justify-items: center; text-align: center; padding: 28px 20px; border-radius: 18px; background: var(--card); }
.gate-card > * { margin: 0; }
.gate-card h1 { margin-top: 4px; font-size: 22px; }
.gate-card .btn-block, .gate-card .key-box { width: 100%; }
.gate-msg { padding: 8px 12px; border-radius: 10px; background: rgb(201 150 47 / 14%); color: var(--warn); font-size: 14px; }
.btn-wx { background: #07c160; color: #fff; border-color: transparent; font-weight: 600; }
.key-box { text-align: left; font-size: 14px; color: var(--muted); }
.key-box summary { cursor: pointer; padding: 6px 0; text-align: center; }
.key-box form { margin-top: 8px; }
