:root {
  --bg: #f6f7f5;
  --surface: #ffffff;
  --surface-2: #eef1ed;
  --text: #1b1f1c;
  --muted: #5d665f;
  --border: #d9ded8;
  --accent: #1f6f4a;
  --accent-text: #ffffff;
  --accent-soft: #dcefe4;
  --gold: #8a6100;
  --gold-soft: #fbefcd;
  --danger: #b3261e;
  --danger-soft: #fbe3e1;
  --focus: #2463d6;
  --radius: 12px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121513;
    --surface: #1b1f1c;
    --surface-2: #252a26;
    --text: #e8ebe8;
    --muted: #a3ada5;
    --border: #343b36;
    --accent: #5fc493;
    --accent-text: #0d1f16;
    --accent-soft: #1f3a2c;
    --gold: #f2c75c;
    --gold-soft: #3a3016;
    --danger: #ff8a80;
    --danger-soft: #3d1f1d;
    --focus: #8ab4ff;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #121513; --surface: #1b1f1c; --surface-2: #252a26; --text: #e8ebe8; --muted: #a3ada5;
  --border: #343b36; --accent: #5fc493; --accent-text: #0d1f16; --accent-soft: #1f3a2c;
  --gold: #f2c75c; --gold-soft: #3a3016; --danger: #ff8a80; --danger-soft: #3d1f1d; --focus: #8ab4ff;
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
p[role="alert"]:empty, p[role="status"]:empty { display: none; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  padding-bottom: env(safe-area-inset-bottom);
}
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

.topbar {
  position: sticky; top: 0; z-index: 5;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: calc(env(safe-area-inset-top) + 10px) 16px 0;
}
.topbar h1 { margin: 0 0 8px; font-size: 1.15rem; display: flex; align-items: center; gap: 8px; }
.topbar h1 img { border-radius: 6px; }
.tabs { display: flex; gap: 4px; overflow-x: auto; }
.tabs button {
  flex: 1 0 auto; min-height: 44px; padding: 0 12px;
  background: none; border: 0; border-bottom: 3px solid transparent;
  color: var(--muted); font-weight: 600; cursor: pointer;
}
.tabs button[aria-current="page"] { color: var(--text); border-bottom-color: var(--accent); }

main { max-width: 760px; margin: 0 auto; padding: 16px; outline: none; }
h2 { font-size: 1.25rem; margin: 4px 0 12px; }
h3 { font-size: 1.05rem; margin: 20px 0 8px; }
.muted { color: var(--muted); }
.small { font-size: 0.875rem; }
.state { padding: 24px 16px; text-align: center; color: var(--muted); background: var(--surface); border: 1px dashed var(--border); border-radius: var(--radius); }
.state.error { color: var(--danger); border-color: var(--danger); background: var(--danger-soft); }

.segmented { display: flex; background: var(--surface-2); border-radius: 10px; padding: 3px; margin-bottom: 10px; }
.segmented button {
  flex: 1; min-height: 40px; border: 0; background: none; border-radius: 8px;
  font-weight: 600; color: var(--muted); cursor: pointer; padding: 0 6px;
}
.segmented button[aria-pressed="true"] { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgba(0,0,0,.12); }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; margin-bottom: 12px; }

table.board { width: 100%; border-collapse: collapse; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.board th, .board td { padding: 10px 8px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: top; }
.board th { font-size: 0.75rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); background: var(--surface-2); }
.board tr:last-child td { border-bottom: 0; }
.board .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.board .rank { width: 2.2em; color: var(--muted); font-variant-numeric: tabular-nums; }
.board .player { font-weight: 600; overflow-wrap: anywhere; }
.board .sub { display: block; font-size: 0.78rem; color: var(--muted); font-weight: 400; }
.board tr.leader td { background: var(--gold-soft); }
.board tr.leader .rank { color: var(--gold); font-weight: 700; }

details.rules { margin-top: 14px; }
details.rules summary { cursor: pointer; font-weight: 600; min-height: 44px; display: flex; align-items: center; }
details.rules ul { margin: 0; padding-left: 20px; }

.result { display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; }
.result .who { font-weight: 600; }
.result .score { font-weight: 700; font-variant-numeric: tabular-nums; text-align: right; }
.result .meta { grid-column: 1 / -1; display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.result .actions { grid-column: 1 / -1; display: flex; flex-wrap: wrap; gap: 8px; margin-top: 6px; }
.tag { font-size: 0.75rem; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); }
.tag.warn { background: var(--gold-soft); color: var(--gold); }
.tag.bad { background: var(--danger-soft); color: var(--danger); }
.tag.good { background: var(--accent-soft); color: var(--accent); }
pre.source {
  grid-column: 1 / -1; margin: 6px 0 0; padding: 10px; background: var(--surface-2); border-radius: 8px;
  white-space: pre-wrap; overflow-wrap: anywhere; font: 0.9rem/1.35 ui-monospace, Menlo, monospace;
}

.btn {
  min-height: 44px; padding: 0 16px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface); font-weight: 600; cursor: pointer;
}
.btn.small { min-height: 36px; padding: 0 12px; font-size: 0.9rem; }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.btn.danger { color: var(--danger); border-color: var(--danger); }
.btn:disabled { opacity: .55; cursor: default; }

form.stack, .stack { display: grid; gap: 12px; }
label.field { display: grid; gap: 4px; font-weight: 600; font-size: 0.92rem; }
label.check { display: flex; gap: 8px; align-items: center; min-height: 44px; }
input[type="text"], input[type="password"], input[type="date"], select, textarea {
  width: 100%; min-height: 44px; padding: 8px 10px; border: 1px solid var(--border); border-radius: 10px; background: var(--surface);
}
input[type="checkbox"] { width: 22px; height: 22px; accent-color: var(--accent); }
textarea { min-height: 140px; font-family: ui-monospace, Menlo, monospace; }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: end; }
.row > * { flex: 1 1 140px; }

dl.kv { display: grid; grid-template-columns: max-content 1fr; gap: 6px 14px; margin: 0; }
dl.kv dt { color: var(--muted); }
dl.kv dd { margin: 0; overflow-wrap: anywhere; }
code { font: 0.9em ui-monospace, Menlo, monospace; background: var(--surface-2); padding: 1px 5px; border-radius: 5px; overflow-wrap: anywhere; }
.status-ok { color: var(--accent); font-weight: 700; }
.status-warn { color: var(--gold); font-weight: 700; }

.badge { display: inline-block; min-width: 1.4em; padding: 0 6px; border-radius: 999px; background: var(--danger); color: #fff; font-size: 0.75rem; line-height: 1.4em; }
.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(16px + env(safe-area-inset-bottom)); max-width: calc(100% - 32px);
  background: var(--text); color: var(--bg); padding: 10px 14px; border-radius: 12px;
  display: flex; gap: 12px; align-items: center; box-shadow: 0 6px 20px rgba(0,0,0,.25); z-index: 10;
}
.toast button { background: none; border: 0; color: inherit; font-weight: 700; text-decoration: underline; min-height: 36px; cursor: pointer; }

.login { max-width: 360px; margin: 10vh auto 0; }
@media (max-width: 420px) {
  main { padding: 12px; }
  .board th, .board td { padding: 9px 6px; }
  .hide-sm { display: none; }
}
