:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --surface-2: #efece4;
  --text: #1d1b16;
  --muted: #6b675d;
  --line: #e4dfd3;
  --accent: #2f6b4f;
  --accent-ink: #ffffff;
  --accent-soft: #e2efe7;
  --warn: #9a5b00;
  --warn-soft: #fbefd9;
  --danger: #b3261e;
  --danger-soft: #fbe4e2;
  --up: #2c7a4b;
  --down: #b3261e;
  --piece-w: #ffffff;
  --piece-b: #1d1b16;
  --radius: 16px;
  --shadow: 0 1px 2px rgba(30, 25, 10, 0.05), 0 6px 20px rgba(30, 25, 10, 0.05);
  --tabs-h: 64px;
  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121311;
    --surface: #1b1d1a;
    --surface-2: #252823;
    --text: #ecebe6;
    --muted: #9b9a90;
    --line: #2f322c;
    --accent: #66c191;
    --accent-ink: #0c1a12;
    --accent-soft: #1d3127;
    --warn: #e7ad57;
    --warn-soft: #382a12;
    --danger: #f07a70;
    --danger-soft: #3a1d1a;
    --up: #74d29c;
    --down: #f07a70;
    --piece-w: #ecebe6;
    --piece-b: #0b0c0a;
    --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 var(--font);
  min-height: 100dvh;
  padding-bottom: calc(var(--tabs-h) + env(safe-area-inset-bottom) + 16px);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}
a { color: var(--accent); text-decoration: none; }
h1, h2 { margin: 0 0 8px; line-height: 1.2; letter-spacing: -0.01em; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.1rem; }
p { margin: 0 0 12px; }
code { font-size: 0.9em; background: var(--surface-2); padding: 1px 5px; border-radius: 5px; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }

/* ---------- Top bar ---------- */
.top {
  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: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(1.4) blur(12px);
  -webkit-backdrop-filter: saturate(1.4) blur(12px);
}
.brand { display: flex; align-items: center; gap: 8px; color: var(--text); font-weight: 700; font-size: 1.05rem; }
.brand-mark {
  display: grid; place-items: center; width: 32px; height: 32px; border-radius: 10px;
  background: var(--accent); color: var(--accent-ink); font-size: 20px;
}
main { max-width: 640px; margin: 0 auto; padding: 4px 16px 16px; outline: none; }

/* ---------- Bottom tabs ---------- */
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: flex;
  height: calc(var(--tabs-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
  border-top: 1px solid var(--line);
}
.tabs[hidden] { display: none; }
body:not(.is-admin) .admin-only { display: none !important; }
.tabs a {
  flex: 1 1 0; position: relative; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  color: var(--muted); font-size: 0.7rem; font-weight: 600;
}
.tabs svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.tabs a.active { color: var(--accent); }
.tabs .tab-cta svg {
  width: 40px; height: 40px; padding: 8px; margin-top: -6px; border-radius: 14px;
  background: var(--accent); color: var(--accent-ink); stroke-width: 2.4;
}
.tabs .tab-cta span { margin-top: -2px; }
.badge {
  position: absolute; top: 6px; left: calc(50% + 6px);
  min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px;
  background: var(--danger); color: #fff; font-size: 0.7rem; line-height: 18px; text-align: center;
}

/* ---------- Cards & layout ---------- */
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 18px 16px; margin-bottom: 14px; box-shadow: var(--shadow);
}
.card.narrow { max-width: 420px; margin: 10vh auto 0; }
.card-head, .page-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; }
.page-head { margin: 8px 2px 12px; align-items: center; }
.page-head h1 { margin: 0; }
.section-title { margin: 22px 4px 10px; }
.sub { font-size: 0.9rem; color: var(--muted); margin: 18px 0 8px; font-weight: 600; }
.empty { color: var(--muted); text-align: center; padding: 18px 8px; margin: 0; }
.center-state { display: grid; place-items: center; min-height: 50vh; }
.spinner {
  width: 32px; height: 32px; border-radius: 50%;
  border: 3px solid var(--line); border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.banner {
  display: flex; justify-content: space-between; gap: 8px; align-items: center;
  margin-bottom: 14px; padding: 14px 16px; border-radius: var(--radius);
  background: var(--accent-soft); color: var(--text); font-weight: 600;
}
.banner.warn { background: var(--warn-soft); }

.info summary { cursor: pointer; font-weight: 600; }
.info p { margin: 10px 0 0; color: var(--muted); font-size: 0.92rem; }
.info h3 { margin: 18px 0 0; font-size: 0.95rem; }
.info ul { margin: 8px 0 0; padding-left: 18px; color: var(--muted); font-size: 0.92rem; display: flex; flex-direction: column; gap: 6px; }
.info .formula {
  color: var(--text); font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 0.85rem;
  background: var(--surface-2); padding: 10px 12px; border-radius: 10px; overflow-wrap: anywhere;
}
.elo-table { width: 100%; margin-top: 10px; border-collapse: collapse; font-size: 0.88rem; font-variant-numeric: tabular-nums; }
.elo-table th, .elo-table td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--line); }
.elo-table th { color: var(--muted); font-weight: 600; }

/* ---------- Leaderboard ---------- */
.rank { list-style: none; margin: 8px -8px 0; padding: 0; }
.rank a {
  display: grid; grid-template-columns: 34px 1fr auto; gap: 10px; align-items: center;
  padding: 12px 8px; border-radius: 12px; color: var(--text);
}
.rank a:active { background: var(--surface-2); }
.rank li + li a { border-top: 1px solid var(--line); border-radius: 0; }
.pos {
  display: grid; place-items: center; width: 30px; height: 30px; border-radius: 50%;
  font-weight: 700; font-size: 0.9rem; color: var(--muted); background: var(--surface-2);
}
.rank li:nth-child(1) .pos { background: #e9c46a; color: #3d2e00; }
.rank li:nth-child(2) .pos { background: #c9ccd1; color: #24272b; }
.rank li:nth-child(3) .pos { background: #d79b6a; color: #3b1f08; }
.who { display: flex; flex-direction: column; min-width: 0; }
.who strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.who small, .elo small { color: var(--muted); font-size: 0.78rem; }
.elo { display: flex; flex-direction: column; align-items: flex-end; font-variant-numeric: tabular-nums; }
.elo strong { font-size: 1.2rem; }
.elo sup { color: var(--muted); font-size: 0.65em; margin-left: 1px; }
.delta { font-variant-numeric: tabular-nums; font-weight: 600; font-size: 0.8rem; }
.delta.up, .up { color: var(--up); }
.delta.down, .down { color: var(--down); }

.chips { list-style: none; padding: 0; margin: 0; display: flex; flex-wrap: wrap; gap: 8px; }
.chips a { display: block; padding: 6px 12px; border-radius: 999px; background: var(--surface-2); color: var(--text); font-size: 0.9rem; }

/* ---------- Games ---------- */
.chips-bar { display: flex; gap: 8px; overflow-x: auto; margin: 0 -16px 12px; padding: 2px 16px 4px; scrollbar-width: none; }
.chips-bar::-webkit-scrollbar { display: none; }
.chip {
  flex: none; padding: 8px 14px; border-radius: 999px; border: 1px solid var(--line);
  background: var(--surface); color: var(--text); font-size: 0.9rem; font-weight: 600;
}
.chip.active { background: var(--text); color: var(--bg); border-color: var(--text); }
.chip b { display: inline-block; min-width: 18px; padding: 0 5px; border-radius: 9px; background: var(--danger); color: #fff; font-size: 0.75rem; text-align: center; }

.game-list { display: flex; flex-direction: column; gap: 10px; }
.game {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 12px 14px; box-shadow: var(--shadow);
}

.game.is-pending { border-left: 4px solid var(--warn); }
.game-meta { display: flex; justify-content: space-between; gap: 8px; font-size: 0.8rem; color: var(--muted); margin-bottom: 8px; flex-wrap: wrap; }
.status { font-weight: 600; }
.status.ok { color: var(--up); }
.status.wait { color: var(--warn); }
.status.bad { color: var(--danger); }
.game-body { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 8px; }
.gp { display: flex; flex-direction: column; align-items: flex-start; gap: 2px; color: var(--text); min-width: 0; }
.game-body .gp:last-child { align-items: flex-end; text-align: right; }
.gp-name { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 100%; }
.gp.won .gp-name { font-weight: 800; }
.result {
  font-weight: 800; font-size: 1.15rem; font-variant-numeric: tabular-nums;
  padding: 4px 10px; border-radius: 10px; background: var(--surface-2);
}
.game-note { margin: 8px 0 0; font-size: 0.88rem; color: var(--muted); }
.game .btn-row { margin-top: 12px; }

.pc {
  display: inline-block; width: 12px; height: 12px; border-radius: 3px; vertical-align: -1px;
  border: 1.5px solid var(--text);
}
.pc-w { background: var(--piece-w); }
.pc-b { background: var(--piece-b); }

/* ---------- Player ---------- */
.player-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; }
.player-head h1 { margin-bottom: 2px; }
.big-elo { font-size: 2.2rem; font-weight: 800; line-height: 1; text-align: right; font-variant-numeric: tabular-nums; color: var(--accent); }
.big-elo small { display: block; font-size: 0.75rem; font-weight: 600; color: var(--muted); margin-top: 4px; }
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin: 16px 0 6px; }
.stats div { background: var(--surface-2); border-radius: 12px; padding: 10px 8px; text-align: center; }
.stats dt { font-size: 0.72rem; color: var(--muted); }
.stats dd { margin: 2px 0 0; font-weight: 700; font-variant-numeric: tabular-nums; }
.spark { margin: 14px 0 0; }
.spark svg { width: 100%; height: 90px; display: block; overflow: visible; }
.spark-line { fill: none; stroke: var(--accent); stroke-width: 2.5; stroke-linejoin: round; stroke-linecap: round; }
.spark-base { stroke: var(--line); stroke-dasharray: 4 4; vector-effect: non-scaling-stroke; }
.spark figcaption { display: flex; justify-content: space-between; font-size: 0.75rem; color: var(--muted); margin-top: 4px; }
.h2h { list-style: none; margin: 0; padding: 0; }
.h2h li { display: flex; justify-content: space-between; padding: 10px 0; border-top: 1px solid var(--line); }
.h2h li:first-child { border-top: 0; }
.wdl { font-variant-numeric: tabular-nums; color: var(--muted); }

/* ---------- Forms ---------- */
.form { display: flex; flex-direction: column; gap: 14px; }
.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field > span { font-size: 0.85rem; font-weight: 600; color: var(--muted); display: flex; align-items: center; gap: 6px; }
input, select, button { font: inherit; color: inherit; }
input:not([type='radio']):not([type='checkbox']), select {
  width: 100%; min-height: 48px; padding: 10px 12px; font-size: 16px;
  background: var(--bg); border: 1px solid var(--line); border-radius: 12px;
  appearance: none; -webkit-appearance: none;
}
select {
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 18px) 21px, calc(100% - 13px) 21px;
  background-size: 5px 5px; background-repeat: no-repeat; padding-right: 32px;
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: 1px; border-color: transparent; }
.form-error { color: var(--danger); font-size: 0.9rem; margin: 0; min-height: 0; }
.form-error:empty { display: none; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.pair { display: grid; grid-template-columns: 1fr auto 1fr; gap: 8px; align-items: end; }
.swap {
  width: 44px; height: 48px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--surface-2); font-size: 1.2rem; cursor: pointer;
}
fieldset { border: 0; padding: 0; margin: 0; min-width: 0; }
legend { font-size: 0.85rem; font-weight: 600; color: var(--muted); padding: 0; margin-bottom: 6px; }
.seg { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.seg legend { grid-column: 1 / -1; }
.seg label { position: relative; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg span {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 12px 4px; border-radius: 12px; border: 1.5px solid var(--line); background: var(--bg); cursor: pointer;
}
.seg b { font-size: 1.25rem; font-variant-numeric: tabular-nums; }
.seg small { font-size: 0.72rem; color: var(--muted); text-align: center; }
.seg input:checked + span { border-color: var(--accent); background: var(--accent-soft); }
.seg input:focus-visible + span { outline: 2px solid var(--accent); }

.check { display: flex; align-items: center; gap: 10px; font-weight: 500; }
.check input { width: 20px; height: 20px; accent-color: var(--accent); }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 10px 16px; border-radius: 12px; border: 1px solid transparent;
  font-weight: 650; font-size: 0.95rem; cursor: pointer; text-align: center; touch-action: manipulation;
}
.btn:disabled { opacity: 0.6; }
.btn.is-busy { position: relative; color: transparent !important; }
.btn.is-busy::after {
  content: ''; position: absolute; width: 18px; height: 18px; border-radius: 50%;
  border: 2px solid currentColor; border-color: var(--muted) var(--muted) var(--muted) transparent;
  animation: spin 0.7s linear infinite;
}
.btn-primary { background: var(--accent); color: var(--accent-ink); }
.btn-primary:active { filter: brightness(0.92); }
.btn-ghost { background: var(--surface-2); color: var(--text); border-color: var(--line); }
.btn-danger-ghost { background: transparent; color: var(--danger); border-color: var(--danger-soft); }
.btn-block { width: 100%; }
.btn-lg { min-height: 54px; font-size: 1.05rem; }
.btn-sm { min-height: 36px; padding: 6px 12px; font-size: 0.85rem; }
.btn-row { display: flex; gap: 8px; }
.btn-row.wrap { flex-wrap: wrap; }
.btn-row > .btn { flex: 1 1 auto; }

/* ---------- Admin ---------- */
.steps { margin: 0 0 16px; padding-left: 20px; display: flex; flex-direction: column; gap: 8px; font-size: 0.95rem; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(var(--tabs-h) + env(safe-area-inset-bottom) + 14px); z-index: 50;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  max-width: calc(100vw - 32px); padding: 12px 18px; border-radius: 12px;
  background: var(--text); color: var(--bg); font-weight: 600; font-size: 0.92rem;
  transition: opacity 0.2s, transform 0.2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.ok { background: var(--up); color: #fff; }
.toast.bad { background: var(--danger); color: #fff; }

@media (min-width: 700px) {
  main { padding-top: 16px; }
  .card { padding: 22px; }
}
@media (max-width: 360px) {
  .row2 { grid-template-columns: 1fr; }
  .stats { grid-template-columns: repeat(2, 1fr); }
}
