/*
 * Sudoku (game): start screen, play view (board, number pad, tools), finish card and dialogs.
 * Tokens come from /melon-shell.css (--m-*); the board has its own --sd-* set so the "paper" design only
 * swaps variables.
 */
:root {
  --sd-card: var(--m-card);
  --sd-fg: var(--m-fg);
  --sd-line: color-mix(in srgb, var(--m-fg) 16%, transparent);
  --sd-strong: color-mix(in srgb, var(--m-fg) 80%, transparent);
  --sd-peer: color-mix(in srgb, var(--m-fg) 5%, var(--m-card));
  --sd-same: color-mix(in srgb, var(--m-primary) 18%, var(--m-card));
  --sd-sel: color-mix(in srgb, var(--m-primary) 34%, var(--m-card));
  --sd-entry: #007a55;
  --sd-note: var(--m-muted-fg);
  --sd-bad: var(--m-danger);
  --sd-glow: #a4f4cf;
  --sd-hint: color-mix(in srgb, #a4f4cf 55%, var(--m-card));
  --sd-font: var(--m-font);
  --sd-weight-given: 600;
  --sd-weight-entry: 500;
  --sd-diag: color-mix(in srgb, var(--m-primary) 7%, var(--sd-card));
  --sd-cage: color-mix(in srgb, var(--sd-fg) 55%, transparent);
}

[data-theme="dark"] {
  --sd-entry: #5ee9b5;
  --sd-glow: rgba(94, 233, 181, 0.45);
  --sd-hint: color-mix(in srgb, #5ee9b5 22%, var(--m-card));
}

/* Paper: cream sheet, serif digits, ink-blue entries – like the puzzle page of a newspaper. */
[data-sd-theme="paper"] {
  --sd-card: #fbf6ea;
  --sd-fg: #1c1917;
  --sd-line: rgba(87, 62, 32, 0.22);
  --sd-strong: #3b2f22;
  --sd-peer: #f3ecdb;
  --sd-same: #e9dfc4;
  --sd-sel: #dccb9e;
  --sd-entry: #1f3f7a;
  --sd-note: #7c6a55;
  --sd-bad: #b91c1c;
  --sd-glow: #f1dfa8;
  --sd-hint: #efe1b4;
  --sd-font: "Source Serif 4", Georgia, "Times New Roman", serif;
  --sd-weight-given: 700;
  --sd-weight-entry: 400;
}

[data-theme="dark"][data-sd-theme="paper"] {
  --sd-card: #26221c;
  --sd-fg: #efe6d2;
  --sd-line: rgba(239, 230, 210, 0.16);
  --sd-strong: rgba(239, 230, 210, 0.75);
  --sd-peer: #2e2922;
  --sd-same: #3b342a;
  --sd-sel: #4b412f;
  --sd-entry: #9cc1ff;
  --sd-note: #a8997f;
  --sd-bad: #f87171;
  --sd-glow: #5c4f33;
  --sd-hint: #4a412c;
}

/* ---------- Start screen ---------- */
.hero-cta { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 14px; margin-top: 18px; }
.btn-xl { height: 48px; padding: 0 22px; border-radius: 12px; font-size: 16px; }
.btn-xl svg { width: 18px; height: 18px; }
.hero-cta .hint { font-size: 13px; }

.home {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 16px;
  align-items: start;
  max-width: 1100px;
}

.home .card + .card { margin-top: 0; }
.home-col { display: grid; gap: 16px; }
.card-head { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.card-head .card-title { margin: 0; }
.card-head .spacer, .spacer { flex: 1 1 auto; }

.continue {
  display: grid;
  gap: 8px;
  width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--m-border);
  border-radius: 12px;
  background: var(--m-card);
  color: var(--m-fg);
  font: inherit;
  text-align: left;
  cursor: pointer;
}

.continue:hover { border-color: var(--m-primary); }
.continue-row { display: flex; align-items: center; gap: 8px; font-size: 14px; }
.continue-row strong { font-weight: 600; }
.continue-row span { margin-left: auto; color: var(--m-muted-fg); font-size: 13px; font-variant-numeric: tabular-nums; }
.progress { height: 5px; border-radius: 99px; background: var(--m-muted); overflow: hidden; }
.progress i { display: block; height: 100%; border-radius: 99px; background: var(--m-primary); }

.levels { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 6px; }

.level-btn {
  display: grid;
  justify-items: center;
  gap: 2px;
  min-width: 0;
  padding: 10px 4px 8px;
  border: 1px solid var(--m-border);
  border-radius: 10px;
  background: var(--m-card);
  color: var(--m-fg);
  font: inherit;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
}

.level-btn:hover { border-color: var(--m-primary); }
.level-btn small { color: var(--m-muted-fg); font-size: 11px; font-weight: 400; font-variant-numeric: tabular-nums; white-space: nowrap; }
.level-btn .dots { display: flex; gap: 3px; height: 6px; }
.level-btn .dots i { width: 6px; height: 6px; border-radius: 50%; background: var(--m-border); }
.level-btn .dots i.on { background: var(--m-primary); }
.level-btn.is-done { border-color: color-mix(in srgb, var(--m-primary) 45%, var(--m-border)); background: var(--m-primary-soft); color: var(--m-primary-soft-fg); }
.level-btn.is-done small { color: var(--m-primary-soft-fg); }
.level-btn.is-going small { color: var(--m-primary-soft-fg); }

.day-nav { display: flex; align-items: center; gap: 2px; }
.day-nav .btn-icon { width: 30px; height: 30px; }
.day-label { min-width: 92px; color: var(--m-muted-fg); font-size: 13px; text-align: center; font-variant-numeric: tabular-nums; }
.streak { display: inline-flex; align-items: center; gap: 4px; color: var(--m-primary-soft-fg); font-size: 13px; font-weight: 600; }
.streak svg { width: 15px; height: 15px; }
.streak.is-off { color: var(--m-muted-fg); font-weight: 500; }
.daily-foot { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-top: 12px; }
.daily-foot .hint { flex: 1 1 180px; }

.stats { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 8px; }
.stat { padding: 10px 8px; border-radius: 10px; background: var(--m-muted); text-align: center; }
.stat b { display: block; font-size: 19px; font-weight: 600; font-variant-numeric: tabular-nums; }
.stat span { color: var(--m-muted-fg); font-size: 11.5px; }
.best-list { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 8px; margin-top: 10px; color: var(--m-muted-fg); font-size: 12px; text-align: center; }
.best-list b { display: block; color: var(--m-fg); font-size: 13px; font-weight: 600; font-variant-numeric: tabular-nums; }

@media (max-width: 420px) {
  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .level-btn { font-size: 12px; padding: 9px 2px 7px; }
}

/* ---------- Play view ---------- */
body.is-play { overflow: hidden; }
body.is-play .page, body.is-play > footer { visibility: hidden; }

.play {
  position: fixed;
  top: var(--play-top, 57px);
  right: 0;
  bottom: var(--play-bottom, 0px);
  left: 0;
  z-index: 15;
  display: flex;
  flex-direction: column;
  background: var(--m-page);
}

.play-head {
  display: flex;
  align-items: center;
  gap: 6px;
  width: min(100%, 1060px);
  margin: 0 auto;
  padding: 8px 12px 6px;
}

.play-title { min-width: 0; overflow: hidden; font-size: 14px; font-weight: 600; text-overflow: ellipsis; white-space: nowrap; }
.play-title small { color: var(--m-muted-fg); font-weight: 500; }
.timer { display: inline-flex; align-items: center; gap: 2px; color: var(--m-muted-fg); font-size: 14px; font-variant-numeric: tabular-nums; }
.timer.is-hidden-time .timer-text { display: none; }
.play-head .btn-icon { width: 34px; height: 34px; }
.play-head .btn-icon svg { width: 18px; height: 18px; }

.play-main {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 0;
  width: min(100%, 1060px);
  margin: 0 auto;
  padding: 0 12px 10px;
}

.board-box { position: relative; flex: none; }

.board {
  --size: 340px;
  display: grid;
  grid-template-columns: repeat(9, minmax(0, 1fr));
  grid-template-rows: repeat(9, minmax(0, 1fr));
  width: var(--size);
  height: var(--size);
  overflow: hidden;
  border: 2.5px solid var(--sd-strong);
  border-radius: 10px;
  background: var(--sd-card);
  outline: none;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.board:focus-visible { box-shadow: 0 0 0 3px var(--m-focus); }

.cell {
  position: relative;
  display: grid;
  place-items: center;
  min-width: 0;
  min-height: 0;
  border-right: 1px solid var(--sd-line);
  border-bottom: 1px solid var(--sd-line);
  background: var(--sd-card);
  color: var(--sd-entry);
  font-family: var(--sd-font);
  font-size: calc(var(--size) / 9 * 0.58);
  font-weight: var(--sd-weight-entry);
  line-height: 1;
  font-variant-numeric: tabular-nums;
  cursor: pointer;
  transition: background-color 0.12s;
}

.cell.br { border-right: 2.5px solid var(--sd-strong); }
.cell.bb { border-bottom: 2.5px solid var(--sd-strong); }
.cell.lc { border-right: 0; }
.cell.lr { border-bottom: 0; }
.cell.is-given { color: var(--sd-fg); font-weight: var(--sd-weight-given); }
.cell.is-diag { background: var(--sd-diag); }
.cell.is-peer { background: var(--sd-peer); }
.cell.is-same { background: var(--sd-same); }
.cell.is-sel { background: var(--sd-sel); }
.cell.is-bad { color: var(--sd-bad); }
.cell.is-wrong { color: var(--sd-bad); text-decoration: underline 2px; text-underline-offset: 3px; }
.cell.is-hint-unit { background: var(--sd-hint); animation: sd-pulse 1.4s ease-in-out infinite; }
.cell.is-hint { box-shadow: inset 0 0 0 3px var(--sd-entry); }
.cell.pop > .v { animation: sd-pop 0.18s ease-out; }

.notes {
  position: absolute;
  inset: 1px;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  grid-template-rows: repeat(3, minmax(0, 1fr));
  color: var(--sd-note);
  font-family: var(--m-font);
  font-size: calc(var(--size) / 9 * 0.24);
  font-weight: 500;
}

.notes span { display: grid; place-items: center; min-width: 0; border-radius: 3px; }
.notes span.is-match { background: var(--sd-same); color: var(--sd-fg); font-weight: 700; }

.cell.wave { animation: sd-wave 0.55s ease-out both; }

@keyframes sd-wave {
  0% { background-color: var(--sd-card); }
  35% { background-color: var(--sd-glow); }
  100% { background-color: var(--sd-card); }
}

@keyframes sd-pop {
  0% { transform: scale(0.6); opacity: 0.4; }
  100% { transform: scale(1); opacity: 1; }
}

@keyframes sd-pulse {
  0%, 100% { background-color: var(--sd-hint); }
  50% { background-color: var(--sd-card); }
}

.pause-cover {
  position: absolute;
  inset: 0;
  z-index: 2;
  display: grid;
  place-items: center;
  align-content: center;
  gap: 10px;
  border-radius: 10px;
  background: var(--sd-card);
  box-shadow: inset 0 0 0 2.5px var(--sd-strong);
  color: var(--sd-fg);
  text-align: center;
}

.pause-cover p { margin: 0; color: var(--m-muted-fg); font-size: 13px; }

.side { display: flex; flex-direction: column; gap: 8px; width: 100%; max-width: 560px; }

.msg {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 38px;
  padding: 6px 10px;
  border-radius: 10px;
  color: var(--m-muted-fg);
  font-size: 13px;
  line-height: 1.35;
}

.msg svg { flex: none; width: 16px; height: 16px; }
.msg.is-hint { background: var(--m-primary-soft); color: var(--m-primary-soft-fg); }
.msg.is-bad { background: color-mix(in srgb, var(--m-danger) 10%, transparent); color: var(--m-danger); }
.msg.is-ok { color: var(--ok); font-weight: 500; }
.msg .btn { flex: none; margin-left: auto; }

.pad { display: grid; grid-template-columns: repeat(9, minmax(0, 1fr)); gap: 5px; }

.pad-btn {
  position: relative;
  display: grid;
  place-items: center;
  align-content: center;
  min-width: 0;
  height: 54px;
  padding: 0;
  border: 1px solid var(--m-border);
  border-radius: 10px;
  background: var(--sd-card);
  color: var(--sd-fg);
  font-family: var(--sd-font);
  font-size: 24px;
  font-weight: 600;
  line-height: 1;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.pad-btn small { margin-top: 3px; color: var(--m-muted-fg); font-family: var(--m-font); font-size: 10px; font-weight: 500; }
.pad-btn:hover { border-color: var(--m-primary); }
.pad-btn:active { transform: scale(0.96); }
.pad-btn.is-complete { opacity: 0.32; }
.pad-btn.is-armed { border-color: var(--m-primary); background: var(--m-primary); color: var(--m-primary-fg); }
.pad-btn.is-armed small { color: var(--m-primary-fg); }
.is-notes .pad-btn { color: var(--m-muted-fg); font-size: 19px; font-weight: 500; }
.is-notes .pad-btn.is-armed { color: var(--m-primary-fg); }

.tools { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 4px; }

.tool {
  position: relative;
  display: grid;
  justify-items: center;
  gap: 3px;
  padding: 6px 2px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--m-muted-fg);
  font: inherit;
  font-size: 12px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.tool svg { width: 22px; height: 22px; }
.tool:hover { background: var(--m-hover); color: var(--m-fg); }
.tool[aria-pressed="true"] { color: var(--m-primary-soft-fg); }
.tool[aria-pressed="true"] .tool-badge { background: var(--m-primary); color: var(--m-primary-fg); }
.tool:disabled { opacity: 0.35; cursor: default; background: transparent; }
.tool-badge { position: absolute; top: 2px; left: calc(50% + 6px); padding: 0 5px; border-radius: 99px; background: var(--m-muted); color: var(--m-muted-fg); font-size: 10px; font-weight: 600; line-height: 16px; }

/* Wide screens: board left, number pad as a 3×3 block beside it. */
@media (min-width: 860px) and (min-aspect-ratio: 5/4) {
  .play-main { flex-direction: row; justify-content: center; align-items: center; gap: 28px; }
  .side { width: 300px; max-width: 300px; }
  .pad { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
  .pad-btn { height: 74px; font-size: 30px; border-radius: 12px; }
  .pad-btn small { font-size: 11px; }
  .msg { min-height: 58px; }
}

@media (max-width: 420px) {
  .play-head { padding: 6px 8px 4px; }
  .play-main { padding: 0 8px 6px; gap: 6px; }
  .board { border-width: 2px; border-radius: 8px; }
  .cell.br { border-right-width: 2px; }
  .cell.bb { border-bottom-width: 2px; }
  .pad { gap: 4px; }
  .pad-btn { height: 48px; font-size: 22px; }
  .tool { font-size: 11px; padding: 4px 2px; }
  .tool svg { width: 20px; height: 20px; }
  .msg { min-height: 34px; font-size: 12.5px; padding: 4px 8px; }
}

@media (max-height: 600px) {
  .pad-btn { height: 44px; }
  .msg { min-height: 30px; }
}

/* Killer cages: dashed outline inset in each cell, sides only where the neighbour belongs to another cage. */
.cage { position: absolute; inset: 0; pointer-events: none; }
.cage.t { border-top: 1.5px dashed var(--sd-cage); top: 3px; }
.cage.b { border-bottom: 1.5px dashed var(--sd-cage); bottom: 3px; }
.cage.l { border-left: 1.5px dashed var(--sd-cage); left: 3px; }
.cage.r { border-right: 1.5px dashed var(--sd-cage); right: 3px; }
.cage-sum {
  position: absolute;
  top: 1px;
  left: 2px;
  z-index: 1;
  padding: 0 2px;
  border-radius: 3px;
  background: inherit;
  color: var(--sd-fg);
  font-family: var(--m-font);
  font-size: max(8px, calc(var(--size) / 9 * 0.2));
  font-weight: 600;
  line-height: 1.1;
}
.board.is-killer .cell { font-size: calc(var(--size) / 9 * 0.52); }
.board.is-killer .notes { inset: 26% 3px 3px 3px; font-size: calc(var(--size) / 9 * 0.2); }
.board.is-killer .cell > .v { margin-top: 12%; }

.variant-seg { margin: 0 0 6px; }
.variant-desc { min-height: 18px; margin: 0 0 10px; }

/* ---------- Finish card ---------- */
.done-layer {
  position: absolute;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 16px;
  background: color-mix(in srgb, var(--m-page) 55%, transparent);
  animation: sd-fade 0.25s ease-out;
}

.done-card { width: min(400px, 100%); max-height: 100%; overflow: auto; text-align: center; }
.done-card h2 { margin: 2px 0 4px; font-size: 22px; font-weight: 700; }
.done-card .card-desc { margin-bottom: 14px; }
.done-badge { display: inline-grid; place-items: center; width: 52px; height: 52px; border-radius: 50%; background: var(--m-primary-soft); color: var(--m-primary-soft-fg); }
.done-badge svg { width: 28px; height: 28px; }
.done-stats { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; margin-bottom: 14px; }
.done-rank { margin: 0 0 14px; padding: 10px 12px; border-radius: 10px; background: var(--m-muted); font-size: 13px; }
.done-rank b { font-size: 15px; }
.done-actions { display: grid; gap: 8px; }
.done-card .status:empty { display: none; }
.done-actions .btn-row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.done-actions .btn-row .btn { width: 100%; }

@keyframes sd-fade { from { opacity: 0; } to { opacity: 1; } }

/* ---------- Dialogs ---------- */
.modal {
  position: fixed;
  inset: 0;
  z-index: 1500;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(12, 10, 9, 0.5);
}

.modal-box { width: min(440px, 100%); max-height: calc(100dvh - 32px); overflow: auto; }
.modal-head { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.modal-head .card-title { margin: 0; }
.modal-head .btn-icon { margin-left: auto; }
.set-group { margin: 14px 0 0; }
.set-group > .label { display: block; margin-bottom: 6px; color: var(--m-muted-fg); font-size: 12px; font-weight: 500; }
.set-group .check { margin: 6px 0; }
.set-group .check small { display: block; color: var(--m-muted-fg); font-size: 12px; }
.set-actions { display: flex; flex-wrap: wrap; gap: 6px; }
.theme-pick { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.theme-opt { position: relative; cursor: pointer; }
.theme-opt input { position: absolute; opacity: 0; pointer-events: none; }
.theme-opt span { display: grid; justify-items: center; gap: 6px; padding: 10px; border: 1px solid var(--m-border); border-radius: 10px; font-size: 13px; font-weight: 500; }
.theme-opt input:checked + span { border-color: var(--m-primary); box-shadow: 0 0 0 1px var(--m-primary); }
.theme-opt input:focus-visible + span { box-shadow: 0 0 0 3px var(--m-focus); }
.theme-swatch { display: grid; place-items: center; width: 54px; height: 40px; border-radius: 6px; font-size: 20px; }
.theme-swatch.std { background: var(--m-card); box-shadow: inset 0 0 0 2px color-mix(in srgb, var(--m-fg) 70%, transparent); color: #007a55; font-family: var(--m-font); font-weight: 500; }
.theme-swatch.paper { background: #fbf6ea; box-shadow: inset 0 0 0 2px #3b2f22; color: #1f3f7a; font-family: "Source Serif 4", Georgia, serif; }

.board-tabs { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); margin: 6px 0 10px; border-radius: 12px; }
.board-tabs span { padding: 5px 4px; font-size: 12.5px; white-space: normal; line-height: 1.25; }
.board-list { margin: 0; padding: 0; list-style: none; }
.board-list li { display: flex; align-items: center; gap: 10px; padding: 7px 4px; border-bottom: 1px solid var(--m-border); font-size: 13.5px; }
.board-list li:last-child { border-bottom: 0; }
.board-list .pos { width: 26px; color: var(--m-muted-fg); font-weight: 600; text-align: right; font-variant-numeric: tabular-nums; }
.board-list .who { flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.board-list .tm { font-variant-numeric: tabular-nums; font-weight: 500; }
.board-list li.is-me { border-radius: 8px; background: var(--m-primary-soft); color: var(--m-primary-soft-fg); }
.board-list li.is-gap { justify-content: center; color: var(--m-muted-fg); }
.board-empty { padding: 18px 0; color: var(--m-muted-fg); font-size: 13px; text-align: center; }
.board-me { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 10px; margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--m-border); font-size: 13px; }
.board-me b { font-weight: 600; }
.board-note { margin: 10px 0 0; color: var(--m-muted-fg); font-size: 12px; }

.loading { display: inline-block; width: 16px; height: 16px; border: 2px solid var(--m-border); border-top-color: var(--m-primary); border-radius: 50%; animation: sd-spin 0.8s linear infinite; vertical-align: -3px; }
@keyframes sd-spin { to { transform: rotate(360deg); } }

@media (prefers-reduced-motion: reduce) {
  .cell, .cell.wave, .cell.pop > .v, .cell.is-hint-unit, .done-layer { animation: none !important; transition: none !important; }
}
