:root {
  --red: #e53935;
  --yellow: #fbc02d;
  --green: #43a047;
  --blue: #1e88e5;
  --red-ink: #c62828;
  --green-ink: #2e7d32;

  --bg: #f4f1ea;
  --surface: #ffffff;
  --surface-2: #f0ece4;
  --text: #1c1c20;
  --muted: #6b6b73;
  --border: #ddd6c9;
  --btn: #ece7dd;
  --btn-text: #1c1c20;
  --shadow: 0 1px 2px rgba(0, 0, 0, .08), 0 2px 8px rgba(0, 0, 0, .06);
  --ok-bg: #e5f4e6;
  --err-bg: #fde7e6;
  --warn-bg: #fff6d6;
  --radius: 14px;
  --nav-h: 64px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --red-ink: #ff6f69;
    --green-ink: #6fcf74;
    --bg: #16161a;
    --surface: #222228;
    --surface-2: #2b2b32;
    --text: #f1f0ec;
    --muted: #a3a3ad;
    --border: #3a3a43;
    --btn: #33333b;
    --btn-text: #f1f0ec;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
    --ok-bg: #1f3522;
    --err-bg: #42201f;
    --warn-bg: #3d3416;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
}
button, input { font: inherit; color: inherit; }
button { cursor: pointer; touch-action: manipulation; }
button:disabled { cursor: not-allowed; }
:focus-visible { outline: 3px solid var(--blue); outline-offset: 2px; }

/* ---------- Шапка ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 10px;
  padding: calc(8px + env(safe-area-inset-top)) 16px 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}
.stripes { display: grid; grid-template-columns: 1fr 1fr; gap: 2px; width: 26px; height: 26px; transform: rotate(-12deg); flex: none; }
.stripes i { border-radius: 3px; }
.stripes i:nth-child(1) { background: var(--red); }
.stripes i:nth-child(2) { background: var(--blue); }
.stripes i:nth-child(3) { background: var(--yellow); }
.stripes i:nth-child(4) { background: var(--green); }
.brand { font-weight: 800; font-size: 20px; letter-spacing: .2px; }
.topinfo { margin-left: auto; color: var(--muted); font-size: 14px; text-align: right; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

main {
  max-width: 480px; margin: 0 auto;
  padding: 16px 16px calc(var(--nav-h) + 24px + env(safe-area-inset-bottom));
  outline: none;
}

/* ---------- Нижняя навигация ---------- */
.bottomnav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: grid; grid-template-columns: repeat(4, 1fr);
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}
.bottomnav button {
  height: var(--nav-h); border: 0; background: none;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  font-size: 12px; color: var(--muted);
}
.bottomnav button .ic { font-size: 22px; line-height: 1; }
.bottomnav button.active { color: var(--text); font-weight: 700; }
.bottomnav button.active .ic { transform: scale(1.1); }
.bottomnav button:disabled { opacity: .35; }

/* ---------- Общие блоки ---------- */
h1 { font-size: 24px; margin: 4px 0 12px; }
h2 { font-size: 18px; margin: 0 0 8px; }
.card {
  background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 14px; margin: 0 0 14px;
}
.hint { color: var(--muted); font-size: 14px; margin: 6px 0 0; }
.muted { color: var(--muted); }

.btn {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  width: 100%; min-height: 56px; padding: 10px 16px;
  border: 0; border-radius: var(--radius);
  background: var(--btn); color: var(--btn-text);
  font-size: 18px; font-weight: 700;
}
.btn.primary { background: var(--green); color: #fff; box-shadow: 0 3px 0 rgba(0, 0, 0, .18); }
.btn.primary:active:not(:disabled) { transform: translateY(2px); box-shadow: 0 1px 0 rgba(0, 0, 0, .18); }
.btn.danger { background: transparent; color: var(--red-ink); border: 2px solid currentColor; min-height: 48px; font-size: 16px; }
.btn.small { min-height: 44px; font-size: 15px; font-weight: 600; }
.btn:disabled { opacity: .45; box-shadow: none; }
.btn + .btn, .btnrow + .btn, .btn + .btnrow { margin-top: 10px; }
.btnrow { display: grid; grid-template-columns: 1fr 2fr; gap: 10px; }
.btnrow .btn + .btn { margin-top: 0; }

.notice { border-radius: 12px; padding: 10px 12px; margin: 0 0 12px; font-size: 15px; }
.notice.ok { background: var(--ok-bg); }
.notice.err { background: var(--err-bg); color: var(--text); border-left: 5px solid var(--red); }
.notice.warn { background: var(--warn-bg); }

/* ---------- Новая игра ---------- */
.players { list-style: none; margin: 0; padding: 0; }
.players li { display: flex; align-items: center; gap: 6px; margin-bottom: 8px; }
.players .num {
  flex: none; width: 28px; height: 28px; border-radius: 50%;
  display: grid; place-items: center; font-weight: 700; font-size: 14px; color: #fff;
}
.players input {
  flex: 1; min-width: 0; height: 48px; padding: 0 12px;
  border: 2px solid var(--border); border-radius: 12px; background: var(--surface-2);
  font-size: 17px;
}
.players input:focus { border-color: var(--blue); outline: none; }
.iconbtn {
  flex: none; width: 40px; height: 48px; border: 0; border-radius: 10px;
  background: var(--btn); font-size: 18px; color: var(--btn-text);
}
.iconbtn:disabled { opacity: .3; }
.iconbtn.del { color: var(--red-ink); }

.stepper { display: flex; align-items: center; gap: 10px; }
.stepper button {
  width: 56px; height: 56px; border-radius: 50%; border: 0;
  background: var(--btn); font-size: 28px; font-weight: 700; line-height: 1;
}
.stepper input, .stepper .stepval {
  width: 84px; height: 56px; text-align: center; font-size: 26px; font-weight: 800;
  border: 2px solid var(--border); border-radius: 12px; background: var(--surface-2);
  display: grid; place-items: center;
}
.stepper input:focus { border-color: var(--blue); outline: none; }

/* ---------- Шапка уровня ---------- */
.levelhead {
  border-radius: var(--radius); padding: 14px 16px; margin-bottom: 12px; color: #fff;
  display: flex; align-items: baseline; justify-content: space-between; gap: 10px; flex-wrap: wrap;
  box-shadow: var(--shadow);
}
.levelhead .lvl { font-size: 26px; font-weight: 800; line-height: 1.1; }
.levelhead .lvl small { font-size: 16px; font-weight: 600; opacity: .9; }
.levelhead .cards { font-size: 18px; font-weight: 700; }
.levelhead .sub { flex-basis: 100%; font-size: 15px; opacity: .95; }
.c0 { background: var(--red); }
.c1 { background: #f9a825; color: #1c1c20; }
.c2 { background: var(--green); }
.c3 { background: var(--blue); }
.levelhead.c1 { color: #1c1c20; }

/* ---------- Строки игроков ---------- */
.prow {
  background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 12px; margin-bottom: 10px; border: 2px solid transparent;
}
.prow.next { border-color: var(--blue); }
.prow .phead { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; min-height: 28px; }
.prow .order {
  flex: none; width: 26px; height: 26px; border-radius: 50%; background: var(--surface-2);
  display: grid; place-items: center; font-size: 14px; font-weight: 700; color: var(--muted);
}
.prow .name { font-weight: 700; font-size: 18px; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.prow .val { margin-left: auto; font-weight: 800; font-size: 20px; flex: none; }
.badge {
  flex: none; font-size: 12px; font-weight: 700; padding: 3px 8px; border-radius: 999px;
  background: var(--yellow); color: #1c1c20; white-space: nowrap;
}
.badge.hit { background: var(--green); color: #fff; }
.badge.miss { background: var(--btn); color: var(--muted); }
.sublabel { font-size: 13px; color: var(--muted); margin: 8px 0 6px; font-weight: 600; }
.sublabel:first-of-type { margin-top: 0; }

.picks { display: grid; grid-template-columns: repeat(auto-fill, minmax(48px, 1fr)); gap: 8px; }
.picks button {
  height: 52px; border: 0; border-radius: 12px; background: var(--btn); color: var(--btn-text);
  font-size: 20px; font-weight: 800;
}
.picks button.on { background: var(--blue); color: #fff; box-shadow: inset 0 0 0 3px rgba(255, 255, 255, .35); }
.picks.taken button.on { background: var(--green); }
.picks.dense { grid-template-columns: repeat(auto-fill, minmax(42px, 1fr)); gap: 6px; }
.picks.dense button { height: 46px; font-size: 18px; }

.sumbar {
  position: sticky; bottom: calc(var(--nav-h) + env(safe-area-inset-bottom) + 8px); z-index: 5;
  background: var(--surface); border-radius: var(--radius); box-shadow: 0 2px 12px rgba(0, 0, 0, .18);
  padding: 12px 14px; margin: 14px 0 12px; font-size: 16px;
  border-left: 6px solid var(--border);
}
.sumbar.ok { border-left-color: var(--green); }
.sumbar.err { border-left-color: var(--red); }
.sumbar b { font-size: 18px; }

/* ---------- Итоги / места ---------- */
.results { list-style: none; margin: 0; padding: 0; }
.results li { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--border); }
.results li:last-child { border-bottom: 0; }
.results .name { font-weight: 700; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.results .bt { color: var(--muted); font-size: 14px; flex: none; }
.results .pts { font-weight: 800; font-size: 18px; min-width: 44px; text-align: right; flex: none; }
.results .place {
  flex: none; width: 30px; height: 30px; border-radius: 50%; display: grid; place-items: center;
  background: var(--surface-2); font-weight: 800;
}
.results li.lead .place { background: var(--yellow); color: #1c1c20; }
.pts.hit { color: var(--green-ink); }
.pts.miss { color: var(--muted); }

.winner { text-align: center; padding: 20px 14px; }
.winner .cup { font-size: 56px; line-height: 1; }
.winner .who { font-size: 26px; font-weight: 800; margin: 8px 0 4px; word-break: break-word; }

/* ---------- Таблица ---------- */
.tablewrap { overflow-x: auto; -webkit-overflow-scrolling: touch; border-radius: var(--radius); box-shadow: var(--shadow); background: var(--surface); margin-bottom: 14px; }
table.score { border-collapse: separate; border-spacing: 0; width: 100%; font-size: 14px; }
.score th, .score td { padding: 6px 6px; text-align: center; border-bottom: 1px solid var(--border); white-space: nowrap; }
.score thead th { position: sticky; top: 0; background: var(--surface-2); font-size: 13px; max-width: 90px; overflow: hidden; text-overflow: ellipsis; }
.score th:first-child, .score td:first-child { position: sticky; left: 0; background: var(--surface); z-index: 1; text-align: left; border-right: 1px solid var(--border); }
.score thead th:first-child { background: var(--surface-2); z-index: 2; }
.score td .p { display: block; font-weight: 800; font-size: 16px; }
.score td .p.hit { color: var(--green-ink); }
.score td .p.miss { color: var(--muted); }
.score td .s { display: block; font-size: 12px; color: var(--muted); }
.score tr.cur td { background: var(--warn-bg); }
.score tr.cur td:first-child { background: var(--warn-bg); }
.score tfoot td { font-weight: 800; font-size: 17px; border-bottom: 0; background: var(--surface-2); }
.score tfoot td:first-child { background: var(--surface-2); }
.score .lead { color: var(--red-ink); }
.score .lvlcell { display: flex; align-items: center; gap: 6px; }
.score .editbtn {
  border: 0; background: var(--btn); border-radius: 8px; width: 32px; height: 32px; font-size: 15px; flex: none;
}

/* ---------- Шпаргалка ---------- */
.rules { margin: 0; padding-left: 20px; }
.rules li { margin-bottom: 6px; }
.ladder { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
.ladder .arr { color: var(--muted); font-weight: 700; }
.ucard {
  display: inline-grid; place-items: center;
  min-width: 40px; height: 56px; padding: 0 6px; border-radius: 8px;
  font-weight: 900; font-size: 18px; color: #fff;
  border: 3px solid #fff; box-shadow: 0 1px 4px rgba(0, 0, 0, .35);
  text-shadow: 0 1px 1px rgba(0, 0, 0, .35);
}
.ucard.sm { font-size: 13px; }
.ucard.k { background: #2b2b32; }
.ucard.r { background: var(--red); }
.ucard.y { background: var(--yellow); color: #1c1c20; text-shadow: none; }
.ucard.g { background: var(--green); }
.ucard.b { background: var(--blue); }
.ucard.w {
  background: conic-gradient(var(--red) 0 25%, var(--blue) 0 50%, var(--green) 0 75%, var(--yellow) 0) ;
}
.ucard.w4 { background: #111; position: relative; }
.ucard.w4::after {
  content: ""; position: absolute; right: 4px; top: 4px; width: 10px; height: 10px; border-radius: 50%;
  background: conic-gradient(var(--red) 0 25%, var(--blue) 0 50%, var(--green) 0 75%, var(--yellow) 0);
}
.colorrow { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; text-align: center; font-size: 13px; color: var(--muted); }
.colorrow .ucard { width: 100%; }
.example { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin-top: 10px; }

/* ---------- Тост ---------- */
.toast {
  position: fixed; left: 50%; top: calc(58px + env(safe-area-inset-top)); z-index: 20;
  transform: translateX(-50%) translateY(-12px); opacity: 0; pointer-events: none;
  max-width: calc(100% - 32px); padding: 12px 16px; border-radius: 12px;
  background: #1c1c20; color: #fff; font-size: 15px; box-shadow: 0 4px 16px rgba(0, 0, 0, .3);
  transition: opacity .2s, transform .2s;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
@media (prefers-color-scheme: dark) { .toast { background: #f1f0ec; color: #1c1c20; } }

@media (prefers-reduced-motion: reduce) { * { transition: none !important; scroll-behavior: auto !important; } }
