:root {
  --bg: #f4f1ea;
  --surface: #ffffff;
  --surface-2: #ece7dc;
  --ink: #1f2326;
  --ink-2: #4c5258;
  --muted: #868c91;
  --line: #d9d3c6;
  --accent: #2f6b4f;
  --accent-ink: #ffffff;
  --accent-soft: #dcebe2;
  --warn: #b2452f;
  --warn-soft: #f6e0da;
  --gold: #b8860b;
  --plus: #2f7d4f;
  --minus: #c0392b;
  --zero: #9aa0a6;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 4px 14px rgba(0,0,0,.06);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15181a;
    --surface: #1f2427;
    --surface-2: #2a3034;
    --ink: #eceae4;
    --ink-2: #b9bcbf;
    --muted: #7f868b;
    --line: #353c41;
    --accent: #5fb38a;
    --accent-ink: #0f1a14;
    --accent-soft: #23382d;
    --warn: #ec7d65;
    --warn-soft: #3d2520;
    --gold: #e0b44a;
    --plus: #5fc98b;
    --minus: #ef6f5f;
    --zero: #7f868b;
    --shadow: 0 1px 2px rgba(0,0,0,.4);
  }
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
a { color: var(--accent); }
h1, h2, h3 { line-height: 1.2; margin: 0 0 .5em; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.15rem; }
.wrap { max-width: 560px; margin: 0 auto; padding: 12px 16px 48px; }
.wide { max-width: 760px; }

/* nav */
nav.top {
  position: sticky; top: 0; z-index: 10; background: var(--surface);
  border-bottom: 1px solid var(--line);
}
nav.top .in { max-width: 760px; margin: 0 auto; display: flex; align-items: center; gap: 2px; padding: 0 8px; overflow-x: auto; }
nav.top .brand { font-weight: 800; letter-spacing: .02em; padding: 12px 8px; color: var(--ink); text-decoration: none; white-space: nowrap; }
nav.top .links { display: flex; margin-left: auto; }
nav.top .links a { padding: 12px 10px; color: var(--ink-2); text-decoration: none; font-size: .95rem; white-space: nowrap; border-bottom: 2px solid transparent; }
nav.top .links a.on { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }

.card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; margin: 12px 0; }
.muted { color: var(--muted); }
.small { font-size: .875rem; }
.center { text-align: center; }
.row { display: flex; gap: 8px; align-items: center; }
.row.wrapr { flex-wrap: wrap; }
.grow { flex: 1; }
.hidden { display: none !important; }

.badge { display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px; border-radius: 999px; font-size: .85rem; font-weight: 600; background: var(--surface-2); color: var(--ink-2); }
.badge.open { background: var(--accent-soft); color: var(--accent); }
.badge.open::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: currentColor; animation: pulse 1.6s infinite; }
@keyframes pulse { 50% { opacity: .25; } }

/* buttons */
button, .btn {
  font: inherit; border: 0; border-radius: 12px; padding: 12px 16px; min-height: 48px;
  background: var(--surface-2); color: var(--ink); font-weight: 600; cursor: pointer;
  touch-action: manipulation; user-select: none; -webkit-user-select: none; text-decoration: none;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
}
button:active:not(:disabled) { transform: scale(.97); }
button:disabled { opacity: .45; cursor: default; }
button.primary, .btn.primary { background: var(--accent); color: var(--accent-ink); }
button.danger { background: var(--warn-soft); color: var(--warn); }
button.ghost { background: transparent; border: 1px solid var(--line); }
button.sm { min-height: 36px; padding: 6px 12px; font-size: .9rem; border-radius: 10px; }
button.block { width: 100%; }

input[type=text], input[type=password], input[type=tel] {
  font: inherit; font-size: 1.1rem; width: 100%; padding: 12px 14px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--surface); color: var(--ink);
}
input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
label.lbl { display: block; font-weight: 600; margin-bottom: 6px; }

/* battlefield grid */
.field { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.cell {
  position: relative; aspect-ratio: 1.25; border-radius: 12px; background: var(--surface-2);
  display: flex; align-items: center; justify-content: center; font-size: 1.9rem; font-weight: 800;
  font-variant-numeric: tabular-nums; border: 2px solid transparent; cursor: pointer;
  transition: background .15s, border-color .15s, transform .15s;
  overflow: hidden;
}
.cell .fill { position: absolute; left: 0; right: 0; bottom: 0; background: var(--accent-soft); transition: height .15s; z-index: 0; }
.cell .v { position: relative; z-index: 1; }
.cell .idx { position: absolute; top: 4px; left: 8px; font-size: .7rem; font-weight: 600; color: var(--muted); z-index: 1; }
.cell.sel { border-color: var(--accent); }
.cell.bad { border-color: var(--warn); background: var(--warn-soft); }
.cell.zero .v { color: var(--muted); font-weight: 600; }

.mini { display: inline-grid; grid-template-columns: repeat(3, auto); gap: 3px; font-variant-numeric: tabular-nums; }
.mini span { min-width: 2.2em; text-align: center; padding: 2px 4px; border-radius: 6px; background: var(--surface-2); font-size: .85rem; font-weight: 600; }
.mini span.z { color: var(--muted); font-weight: 400; }
.mini span.p { background: var(--accent-soft); color: var(--plus); }
.mini span.m { background: var(--warn-soft); color: var(--minus); }

/* counter */
.remain { display: flex; align-items: baseline; justify-content: space-between; margin: 4px 0 8px; }
.remain .n { font-size: 2.2rem; font-weight: 800; font-variant-numeric: tabular-nums; }
.remain.over .n { color: var(--warn); }
.remain.done .n { color: var(--accent); }
.bar { height: 8px; background: var(--surface-2); border-radius: 99px; overflow: hidden; }
.bar > div { height: 100%; background: var(--accent); transition: width .15s; }
.remain.over + .bar > div { background: var(--warn); }

.pad { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-top: 12px; }
.pad button { font-size: 1.25rem; min-height: 56px; }
.pad button.m { color: var(--minus); }
.pad button.p { color: var(--plus); }
.tools { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 8px; }
.tools button { font-size: .9rem; padding: 10px 6px; }

.switch { display: flex; align-items: center; gap: 10px; cursor: pointer; font-size: .95rem; }
.switch input { width: 20px; height: 20px; accent-color: var(--accent); }

.msg { padding: 10px 14px; border-radius: 12px; margin: 10px 0; font-size: .95rem; }
.msg.err { background: var(--warn-soft); color: var(--warn); }
.msg.ok { background: var(--accent-soft); color: var(--accent); }

/* leaderboard */
.lb { list-style: none; padding: 0; margin: 0; }
.lb li { border-bottom: 1px solid var(--line); }
.lb li:last-child { border-bottom: 0; }
.lb .r { display: flex; align-items: center; gap: 10px; padding: 10px 2px; cursor: pointer; }
.lb .rk { width: 2.2em; text-align: center; font-weight: 800; font-variant-numeric: tabular-nums; color: var(--ink-2); }
.lb .nm { flex: 1; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lb .sc { font-variant-numeric: tabular-nums; font-size: .9rem; color: var(--ink-2); white-space: nowrap; }
.lb .sc b { color: var(--ink); }
.lb li.me { background: var(--accent-soft); border-radius: 10px; }
.lb li.top1 .rk { color: var(--gold); }
.lb .det { padding: 4px 8px 14px 3.4em; display: none; }
.lb li.x .det { display: block; }

table.t { width: 100%; border-collapse: collapse; font-size: .9rem; font-variant-numeric: tabular-nums; }
table.t th, table.t td { padding: 8px 6px; border-bottom: 1px solid var(--line); text-align: right; }
table.t th:first-child, table.t td:first-child, table.t th:nth-child(2), table.t td:nth-child(2) { text-align: left; }
table.t th { color: var(--muted); font-weight: 600; font-size: .8rem; }
.scroll-x { overflow-x: auto; }

select { font: inherit; padding: 10px 12px; border-radius: 12px; border: 1px solid var(--line); background: var(--surface); color: var(--ink); }

dialog { border: 0; border-radius: var(--radius); padding: 20px; background: var(--surface); color: var(--ink); width: min(92vw, 420px); box-shadow: 0 10px 40px rgba(0,0,0,.3); }
dialog::backdrop { background: rgba(0,0,0,.45); }

.toast { position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 12px; z-index: 100; font-size: .95rem; max-width: 90vw; }
.badge { white-space: nowrap; }
@media (max-width: 400px) {
  nav.top .brand span { display: none; }
  nav.top .links a { padding: 12px 8px; }
  h1 { font-size: 1.3rem; }
}
