:root {
  --bg: #f3efe6;
  --surface: #fffdf8;
  --surface-2: #f7f3ea;
  --line: #e2dccd;
  --text: #1c2621;
  --muted: #65706a;
  --felt: #1f5c43;
  --felt-strong: #174a35;
  --felt-soft: #e2efe8;
  --on-felt: #ffffff;
  --red: #b3372f;
  --red-soft: #f8e5e2;
  --gold: #9a6b12;
  --gold-soft: #f6ecd6;
  --shadow: 0 1px 2px rgb(30 40 35 / 6%), 0 4px 16px rgb(30 40 35 / 6%);
  --radius: 14px;
  --font: "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", Arial, sans-serif;
  --num: ui-rounded, "SF Pro Rounded", "Segoe UI", system-ui, sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111613;
    --surface: #19201c;
    --surface-2: #1f2823;
    --line: #2d3832;
    --text: #e9eee9;
    --muted: #9aa69f;
    --felt: #3f9c72;
    --felt-strong: #4fb486;
    --felt-soft: #1d3329;
    --on-felt: #08130d;
    --red: #ef7d72;
    --red-soft: #3a2220;
    --gold: #e0b45c;
    --gold-soft: #33291a;
    --shadow: 0 1px 2px rgb(0 0 0 / 30%), 0 6px 20px rgb(0 0 0 / 25%);
    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;
}
button, input, select { font: inherit; color: inherit; }
a { color: var(--felt); }
:focus-visible { outline: 3px solid color-mix(in srgb, var(--felt) 55%, transparent); outline-offset: 2px; }

.wrap { max-width: 880px; margin: 0 auto; padding: 0 16px calc(32px + env(safe-area-inset-bottom)); }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 5;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.topbar-in { max-width: 880px; margin: 0 auto; padding: 10px 16px; display: flex; align-items: center; gap: 12px; }
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; color: var(--text); font-weight: 700; letter-spacing: -0.01em; }
.brand-mark {
  width: 32px; height: 32px; border-radius: 9px; display: grid; place-items: center;
  background: var(--felt); color: var(--on-felt); font-size: 18px;
}
.topbar .spacer { flex: 1; }
.who { color: var(--muted); font-size: 14px; max-width: 40vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 44px; padding: 0 18px; border-radius: 11px; border: 1px solid var(--line);
  background: var(--surface); cursor: pointer; font-weight: 600; text-decoration: none; color: var(--text);
  transition: background .15s, border-color .15s, transform .05s;
}
.btn:hover { background: var(--surface-2); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .55; cursor: default; }
.btn-primary { background: var(--felt); border-color: var(--felt); color: var(--on-felt); }
.btn-primary:hover { background: var(--felt-strong); }
.btn-danger { color: var(--red); }
.btn-danger:hover { background: var(--red-soft); border-color: var(--red-soft); }
.btn-ghost { border-color: transparent; background: transparent; }
.btn-ghost:hover { background: var(--surface-2); }
.btn-sm { min-height: 36px; padding: 0 12px; font-size: 14px; border-radius: 9px; }
.btn-block { width: 100%; }
.icon-btn { width: 40px; padding: 0; }

/* ---------- forms ---------- */
.field { display: grid; gap: 6px; margin-bottom: 14px; }
.field label, .label { font-weight: 600; font-size: 14px; }
.hint { color: var(--muted); font-size: 13px; }
.input {
  width: 100%; min-height: 46px; padding: 0 14px; border-radius: 11px;
  border: 1px solid var(--line); background: var(--surface);
}
.input:focus { border-color: var(--felt); outline: none; box-shadow: 0 0 0 3px color-mix(in srgb, var(--felt) 22%, transparent); }
.input[aria-invalid="true"] { border-color: var(--red); }
.error-text { color: var(--red); font-size: 14px; }
.form-error { background: var(--red-soft); color: var(--red); padding: 10px 12px; border-radius: 10px; margin-bottom: 14px; font-size: 14px; }
.notice { background: var(--felt-soft); padding: 12px 14px; border-radius: 10px; margin-bottom: 14px; }

/* ---------- auth ---------- */
.auth { min-height: 100dvh; display: grid; place-items: center; padding: 24px 16px; }
.auth-card { width: 100%; max-width: 400px; background: var(--surface); border: 1px solid var(--line); border-radius: 18px; padding: 28px 22px; box-shadow: var(--shadow); }
.auth-head { display: flex; align-items: center; gap: 12px; margin-bottom: 20px; }
.auth-head .brand-mark { width: 44px; height: 44px; font-size: 24px; border-radius: 12px; }
.auth h1 { font-size: 22px; margin: 0; letter-spacing: -0.01em; }
.auth .sub { color: var(--muted); margin: 2px 0 0; font-size: 14px; }
.auth-links { display: flex; justify-content: space-between; flex-wrap: wrap; gap: 8px; margin-top: 16px; font-size: 14px; }
.suits { letter-spacing: 4px; color: var(--muted); font-size: 13px; text-align: center; margin-top: 18px; }
.suits .r { color: var(--red); }

/* ---------- page headers ---------- */
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; margin: 24px 0 16px; flex-wrap: wrap; }
.page-head h1 { margin: 0; font-size: 26px; letter-spacing: -0.02em; line-height: 1.15; }
.back { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); text-decoration: none; font-size: 14px; margin-top: 16px; }
.back:hover { color: var(--text); }

/* ---------- tabs ---------- */
.tabs { display: flex; gap: 4px; background: var(--surface-2); padding: 4px; border-radius: 12px; margin-bottom: 16px; width: fit-content; }
.tab { border: 0; background: transparent; padding: 8px 14px; border-radius: 9px; cursor: pointer; font-weight: 600; color: var(--muted); }
.tab[aria-selected="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.count { font-weight: 500; opacity: .7; margin-left: 4px; }

/* ---------- game list ---------- */
.game-list { display: grid; gap: 10px; }
.game-card {
  display: grid; grid-template-columns: 1fr auto; gap: 8px 12px; align-items: start;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 14px 14px 16px;
  box-shadow: var(--shadow);
}
.game-card a.title { font-weight: 700; color: var(--text); text-decoration: none; font-size: 17px; }
.game-card a.title::after { content: ""; position: absolute; inset: 0; }
.game-card { position: relative; }
.game-card .actions { position: relative; z-index: 1; }
.game-meta { color: var(--muted); font-size: 13px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; grid-column: 1 / -1; }
.chip { display: inline-flex; align-items: center; gap: 6px; padding: 3px 10px; border-radius: 999px; background: var(--surface-2); font-size: 13px; border: 1px solid var(--line); }
.chip b { font-variant-numeric: tabular-nums; }
.chip.out { opacity: .55; text-decoration: line-through; }
.chip.win { background: var(--gold-soft); border-color: transparent; color: var(--gold); font-weight: 600; }
.empty { text-align: center; padding: 40px 16px; color: var(--muted); background: var(--surface); border: 1px dashed var(--line); border-radius: var(--radius); }
.empty .big { font-size: 34px; letter-spacing: 6px; margin-bottom: 6px; }

/* ---------- game screen ---------- */
.rules { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.rule { font-size: 13px; padding: 3px 10px; border-radius: 999px; background: var(--felt-soft); color: var(--felt-strong); font-weight: 600; }
.rule.off { background: var(--surface-2); color: var(--muted); font-weight: 500; }

.banner { display: flex; flex-wrap: wrap; align-items: center; gap: 12px; justify-content: space-between; padding: 14px 16px; border-radius: var(--radius); background: var(--gold-soft); color: var(--text); margin-bottom: 16px; }
.banner strong { color: var(--gold); }
.banner-finish { flex-direction: column; align-items: stretch; gap: 10px; padding: 16px; border: 1px solid color-mix(in srgb, var(--gold) 35%, transparent); }
.banner-title { font-size: 20px; font-weight: 800; letter-spacing: -0.01em; }
.banner-text { font-size: 16px; }
.banner-go { width: 100%; }
.linkish { border: 0; background: none; padding: 0; color: var(--felt); font-weight: 600; text-decoration: underline; cursor: pointer; }

.quick {
  display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  background: var(--felt-soft); border: 1px solid color-mix(in srgb, var(--felt) 30%, transparent);
  border-radius: var(--radius); padding: 14px 16px; margin-bottom: 16px;
}
.quick-text { min-width: 0; flex: 1 1 220px; }
.quick-title { font-weight: 700; font-size: 17px; }
.quick-btn { min-height: 52px; padding: 0 26px; font-size: 17px; }
@media (max-width: 520px) { .quick-btn { width: 100%; } }

.players { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 10px; }
.player {
  position: relative; text-align: left; cursor: pointer;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 14px 12px;
  box-shadow: var(--shadow); min-height: 120px; display: flex; flex-direction: column; gap: 4px;
  transition: border-color .15s, transform .08s;
}
.player:hover:not(:disabled) { border-color: var(--felt); }
.player:active:not(:disabled) { transform: scale(.985); }
.player .pname { font-weight: 700; font-size: 16px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.player .pscore { font-family: var(--num); font-size: 44px; font-weight: 700; line-height: 1; letter-spacing: -0.03em; font-variant-numeric: tabular-nums; margin-top: 6px; }
.player .pnote { font-size: 13px; color: var(--muted); margin-top: auto; }
.player.out { opacity: .6; cursor: default; box-shadow: none; background: var(--surface-2); }
.player.out .pscore { text-decoration: line-through; text-decoration-thickness: 3px; }
.player.winner { border-color: var(--gold); background: var(--gold-soft); }
.player.flash { animation: flash .9s ease-out; }
@keyframes flash { from { box-shadow: 0 0 0 4px color-mix(in srgb, var(--felt) 45%, transparent); } to { box-shadow: var(--shadow); } }
.badge { position: absolute; top: 10px; right: 10px; font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; padding: 2px 8px; border-radius: 999px; }
.badge.out { background: var(--red-soft); color: var(--red); }
.badge.win { background: var(--gold); color: var(--surface); }
.badge.near { background: var(--felt-soft); color: var(--felt-strong); }

.last-op {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap; justify-content: space-between;
  margin: 16px 0; padding: 12px 14px; border-radius: var(--radius); background: var(--surface); border: 1px solid var(--line);
}
.last-op .what { font-size: 15px; }
.last-op .what .muted { color: var(--muted); }
.last-op .btns { display: flex; gap: 6px; }

.section-title { font-size: 15px; font-weight: 700; margin: 26px 0 10px; display: flex; justify-content: space-between; align-items: center; }
.history { list-style: none; margin: 0; padding: 0; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.history li { display: grid; grid-template-columns: 1fr auto; gap: 2px 12px; padding: 10px 14px; border-top: 1px solid var(--line); font-size: 14px; }
.history li:first-child { border-top: 0; }
.history .delta { font-weight: 700; font-variant-numeric: tabular-nums; }
.history .delta.add { color: var(--felt-strong); }
.history .delta.sub { color: var(--red); }
.history .flow { color: var(--muted); font-variant-numeric: tabular-nums; }
.history .time { color: var(--muted); font-size: 12px; grid-column: 1 / -1; }
.tag { font-size: 11px; font-weight: 700; padding: 1px 7px; border-radius: 999px; margin-left: 6px; vertical-align: 1px; }
.tag.reset { background: var(--felt-soft); color: var(--felt-strong); }
.tag.eliminated { background: var(--red-soft); color: var(--red); }
.tag.edited { background: var(--surface-2); color: var(--muted); }

.game-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 26px; }

/* ---------- new game ---------- */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px 16px; box-shadow: var(--shadow); margin-bottom: 14px; }
.card h2 { font-size: 17px; margin: 0 0 4px; }
.card > .hint { margin: 0 0 14px; }
.player-row { display: flex; gap: 8px; margin-bottom: 8px; align-items: center; }
.player-row .num { width: 22px; color: var(--muted); font-size: 14px; text-align: right; flex: none; }
.threshold { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; align-items: center; padding: 12px 0; border-top: 1px solid var(--line); }
.threshold:first-of-type { border-top: 0; padding-top: 0; }
.threshold input[type="checkbox"] { width: 22px; height: 22px; accent-color: var(--felt); }
.threshold .t-body { display: grid; gap: 6px; }
.threshold .t-row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.threshold .input { max-width: 140px; }
.sticky-submit { position: sticky; bottom: 0; padding: 12px 0 calc(12px + env(safe-area-inset-bottom)); background: linear-gradient(to top, var(--bg) 70%, transparent); }

/* ---------- dialogs ---------- */
dialog {
  border: 0; padding: 0; background: var(--surface); color: var(--text);
  width: min(440px, calc(100vw - 24px)); border-radius: 20px; box-shadow: 0 20px 60px rgb(0 0 0 / 30%);
}
dialog::backdrop { background: rgb(10 16 13 / 45%); backdrop-filter: blur(2px); }
@media (max-width: 560px) {
  dialog.sheet { margin: auto 0 0; width: 100vw; max-width: 100vw; border-radius: 20px 20px 0 0; padding-bottom: env(safe-area-inset-bottom); }
}
.dlg { padding: 20px 18px 18px; }
.dlg-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
.dlg-head h2 { margin: 0; font-size: 20px; }
.dlg-head .hint { margin-top: 2px; }
.segmented { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; background: var(--surface-2); padding: 4px; border-radius: 12px; margin-bottom: 12px; }
.segmented button { border: 0; min-height: 44px; border-radius: 9px; background: transparent; font-weight: 700; cursor: pointer; color: var(--muted); }
.segmented button[aria-pressed="true"].add { background: var(--felt); color: var(--on-felt); }
.segmented button[aria-pressed="true"].sub { background: var(--red); color: var(--surface); }
.amount { width: 100%; height: 72px; text-align: center; font-family: var(--num); font-size: 40px; font-weight: 700; border-radius: 14px; border: 1px solid var(--line); background: var(--surface-2); font-variant-numeric: tabular-nums; }
.amount:focus { outline: none; border-color: var(--felt); box-shadow: 0 0 0 3px color-mix(in srgb, var(--felt) 22%, transparent); }
.preview { min-height: 24px; margin: 10px 0 14px; text-align: center; color: var(--muted); font-variant-numeric: tabular-nums; }
.preview strong { color: var(--text); }
.preview .warn { color: var(--red); font-weight: 600; }
.preview .good { color: var(--felt-strong); font-weight: 600; }
.dlg-actions { display: flex; gap: 8px; }
.dlg-actions .btn { flex: 1; }

/* ---------- toasts ---------- */
#toasts { position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(18px + env(safe-area-inset-bottom)); z-index: 50; display: grid; gap: 8px; width: min(420px, calc(100vw - 32px)); pointer-events: none; }
.toast { background: var(--text); color: var(--bg); padding: 12px 16px; border-radius: 12px; box-shadow: var(--shadow); font-size: 14px; animation: toast-in .2s ease-out; }
.toast.err { background: var(--red); color: #fff; }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }

.loading { padding: 60px 0; text-align: center; color: var(--muted); }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

@media (max-width: 520px) {
  .who { display: none; }
  .brand { white-space: nowrap; }
}
@media (max-width: 420px) {
  .players { grid-template-columns: 1fr 1fr; }
  .player .pscore { font-size: 38px; }
  .page-head h1 { font-size: 22px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
