:root {
  --ink: #111;
  --muted: #6b6b6b;
  --line: #d4d4d4;
  --thick: #111;
  --given-bg: #e4e4e4;
  --given-ink: #6f6f6f;
  --user-ink: #1d4ed8;
  --peer: #f2f5fb;
  --same: #dbe5fb;
  --sel: #b9cdf7;
  --bad: #dc2626;
  --bad-bg: #fde8e8;
  --chip: #efefef;
  --radius: 12px;
  --cell: min(64px, calc((100vw - 28px) / var(--n)));
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  color: var(--ink);
  background: #fff;
}
.big { --cell: min(52px, calc((100vw - 28px) / var(--n))); }
[hidden] { display: none !important; }

* { box-sizing: border-box; }
body { margin: 0; }
button { font: inherit; color: inherit; cursor: pointer; }

.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 320px;
  gap: 40px;
  max-width: 1100px;
  margin: 0 auto;
  padding: 28px 20px 60px;
  align-items: start;
}
@media (max-width: 900px) {
  .layout { grid-template-columns: minmax(0, 1fr); gap: 28px; padding: 18px 12px 40px; }
  .ranking { position: static; }
}

.game { display: flex; flex-direction: column; align-items: center; }
.date { margin: 0 0 18px; font-size: 15px; }

.head { display: flex; flex-direction: column; align-items: center; gap: 14px; }
h1 {
  margin: 0;
  font: 900 clamp(38px, 7vw, 60px)/1 "Arial Black", "Helvetica Neue", Arial, sans-serif;
  letter-spacing: .5px;
}
.pickers { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; align-items: center; }

.seg { display: inline-flex; background: var(--chip); border-radius: 10px; padding: 3px; gap: 2px; }
.seg button {
  border: 0; background: transparent; padding: 6px 12px; border-radius: 8px;
  font-size: 15px; color: #333; transition: background .15s, box-shadow .15s;
}
.seg button:hover { background: #e2e2e2; }
.seg button[aria-checked="true"] { background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.15); font-weight: 600; color: #000; }
.seg.small button { font-size: 13px; padding: 5px 10px; }

.help-btn {
  width: 24px; height: 24px; border-radius: 50%; border: 0; background: var(--chip);
  font-size: 13px; font-weight: 700; color: #555;
}

.status { margin: 18px 0 26px; display: flex; gap: 8px; align-items: center; min-height: 28px; }
.timer {
  background: #000; color: #fff; font: 700 14px/1 ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  padding: 7px 8px; border-radius: 4px; letter-spacing: .5px;
}
.penalty { font-size: 13px; color: var(--bad); font-weight: 600; }

/* ---------- board ---------- */
.board-wrap { position: relative; }
.board {
  display: grid;
  grid-template-columns: repeat(var(--n), var(--cell));
  grid-template-rows: repeat(var(--n), var(--cell));
  border: 1px solid #555;
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: 0 6px 18px rgba(0,0,0,.12);
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  outline: none;
}
.cell {
  position: relative;
  display: flex; align-items: center; justify-content: center;
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  font-size: calc(var(--cell) * .4);
  font-weight: 700;
  color: var(--user-ink);
  background: #fff;
  cursor: pointer;
  transition: background .08s;
}
.cell.br { border-right: 3px solid var(--thick); }
.cell.bb { border-bottom: 3px solid var(--thick); }
.cell.lastc { border-right: 0; }
.cell.lastr { border-bottom: 0; }

.cell.given { background: var(--given-bg); color: var(--given-ink); }
.cell.peer { background: var(--peer); }
.cell.given.peer { background: #dcdfe6; }
.cell.same { background: var(--same); }
.cell.sel { background: var(--sel); }
.cell.conflict { color: var(--bad); }
.cell.conflict:not(.given) { background: var(--bad-bg); }
.cell.sel.conflict { background: #f9c9c9; }
.cell.wrong { color: var(--bad); text-decoration: line-through 2px; }
.cell.hinted { color: #059669; }
.cell.pop { animation: pop .25s ease; }
@keyframes pop { 50% { transform: scale(1.18); } }

.notes {
  position: absolute; inset: 2px;
  display: grid;
  grid-template-columns: repeat(var(--bc), 1fr);
  grid-template-rows: repeat(var(--br), 1fr);
  font-size: calc(var(--cell) * .2);
  font-weight: 500; color: #666; line-height: 1;
}
.notes span { display: flex; align-items: center; justify-content: center; }

.board.solved .cell { animation: wave .6s ease both; }
@keyframes wave { 40% { background: #c7f0d6; } }

.loading {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  background: rgba(255,255,255,.8); border-radius: var(--radius); font-weight: 600; color: var(--muted);
}

/* ---------- pad & tools ---------- */
.pad { display: flex; flex-wrap: nowrap; justify-content: center; gap: 6px; margin-top: 40px; max-width: 560px; }
.key {
  width: min(48px, calc((100vw - 24px - 6px * var(--n)) / (var(--n) + 1)));
  height: 48px; border: 0; border-radius: 8px; padding: 0;
  background: var(--chip); color: #444; font-size: 18px;
  transition: background .12s, transform .06s;
}
.key:hover:not(:disabled) { background: #e0e0e0; }
.key:active:not(:disabled) { transform: scale(.94); }
.key:disabled { color: #c4c4c4; cursor: default; }
.key svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; vertical-align: middle; }
.notes-on .key:not(.del) { color: var(--user-ink); }

.tools { display: flex; flex-wrap: wrap; justify-content: center; gap: 6px; margin-top: 18px; }
.tool {
  display: inline-flex; align-items: center; gap: 6px;
  border: 1px solid #e3e3e3; background: #fff; border-radius: 999px;
  padding: 7px 13px; font-size: 13px; color: #333;
}
.tool:hover { background: #f6f6f6; }
.tool svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.tool[aria-pressed="true"] { background: #111; color: #fff; border-color: #111; }

/* ---------- ranking ---------- */
.ranking {
  border: 1px solid #ececec; border-radius: 16px; padding: 20px;
  box-shadow: 0 4px 14px rgba(0,0,0,.05);
  position: sticky; top: 20px;
}
.ranking-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; margin-bottom: 12px; }
.ranking h2 { margin: 0; font-size: 20px; font-weight: 800; }
.ranking-sub { font-size: 13px; color: var(--muted); }
.rank-list { list-style: none; margin: 14px 0 0; padding: 0; counter-reset: r; }
.rank-list li {
  counter-increment: r;
  display: grid; grid-template-columns: 28px 1fr auto; align-items: center; gap: 8px;
  padding: 8px 6px; border-radius: 8px; font-size: 14px;
}
.rank-list li:nth-child(odd) { background: #fafafa; }
.rank-list li::before { content: counter(r); font-weight: 700; color: #999; text-align: center; }
.rank-list li:nth-child(1)::before { content: "🥇"; }
.rank-list li:nth-child(2)::before { content: "🥈"; }
.rank-list li:nth-child(3)::before { content: "🥉"; }
.rank-list li.me { background: #eaf0fd; outline: 1px solid #bcd0fa; }
.rank-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.rank-name small { font-weight: 400; color: var(--muted); margin-left: 4px; }
.rank-time { font: 600 13px ui-monospace, Menlo, Consolas, monospace; }
.rank-empty { color: var(--muted); font-size: 14px; text-align: center; margin: 24px 0 8px; }

/* ---------- dialogs ---------- */
dialog {
  border: 0; border-radius: 18px; padding: 28px; width: min(400px, 92vw);
  box-shadow: 0 20px 60px rgba(0,0,0,.25);
}
dialog::backdrop { background: rgba(0,0,0,.35); backdrop-filter: blur(2px); }
dialog h2 { margin: 0 0 8px; font-size: 26px; font-weight: 900; text-align: center; }
dialog p { text-align: center; }
.confetti { font-size: 46px; text-align: center; }
.win-time { font: 800 34px ui-monospace, Menlo, Consolas, monospace; margin: 4px 0; }
.win-meta { color: var(--muted); font-size: 14px; margin: 0 0 18px; }
.name-label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 6px; }
#nameInput {
  width: 100%; padding: 11px 12px; font-size: 16px;
  border: 1px solid #ccc; border-radius: 10px; outline: none;
}
#nameInput:focus { border-color: #111; }
.win-result { font-weight: 700; font-size: 16px; }
.dlg-actions { display: flex; gap: 8px; justify-content: center; margin-top: 18px; }
.btn { white-space: nowrap; border: 1px solid #ddd; background: #fff; border-radius: 10px; padding: 10px 16px; font-weight: 600; }
.btn.primary { background: #111; color: #fff; border-color: #111; }
.btn:disabled { opacity: .5; cursor: default; }
.help { padding-left: 18px; line-height: 1.5; font-size: 14px; }
.help li { margin-bottom: 6px; }
kbd { background: #f1f1f1; border: 1px solid #ccc; border-radius: 4px; padding: 0 5px; font-size: 12px; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px);
  background: #111; color: #fff; padding: 10px 16px; border-radius: 10px; font-size: 14px;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
