:root {
  --cell: 30px;
  --rowclue: 66px;
  --colclue: 66px;
  --clue-font: 13px;
  --cell-gap: 3px;         /* space between rounded cells */

  --bg: #ffffff;
  --ink: #33404f;          /* dark text */
  --title: #2a2550;
  --blue: #5a49d6;         /* accent: icons, toggles, filled cells, brand (darker purple) */
  --blue-hi: #1fa8ff;      /* gradient start (matches the app icon) */
  --blue-lo: #6f5af0;      /* gradient end */
  --page-bg: linear-gradient(180deg, #ffffff 0%, #eef0fb 100%);
  --cell-empty: #e7edf9;   /* empty rounded cell */
  --sep: #565b66; /* dashed group divider (every 5), dark gray */
  --clue-ink: #9aa0b2;     /* clue numbers (soft, light gray) */
  --clue-done: #c9c9d6;
  --cross: #48506a;        /* X mark */
  --hl: rgba(106, 92, 240, 0.13);
  --heart: #f5333f;
  --heart-lost: #dbd8ec;
  --panel: #eeecf8;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  min-height: 100%;
  /* Same full-screen backdrop as the menu, so the menu and game screens match. */
  background: var(--page-bg);
  background-attachment: fixed;
  color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  -webkit-user-select: none;
  user-select: none;
}

/* --- Main menu page --- */
.menu-page {
  position: fixed;
  inset: 0;
  z-index: 15;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: var(--page-bg);
}
.menu-page.hidden { display: none; }
#appPage.hidden { display: none; }
.menu-inner {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  max-width: 360px;
  text-align: center;
}
.hero-logo {
  width: 128px;
  height: auto;
  filter: drop-shadow(0 12px 24px rgba(106, 92, 240, 0.32));
}
.brand {
  font-size: 30px;
  font-weight: 800;
  letter-spacing: 0.5px;
  color: var(--blue);
  margin: 12px 0 40px;
}
.btn.big-blue {
  width: 100%;
  font-size: 17px;
  font-weight: 800;
  padding: 15px;
  border-radius: 13px;
  /* Wide 3-stop gradient so shifting its position reads as a moving gradient. */
  background-image: linear-gradient(120deg, var(--blue-hi), var(--blue-lo), var(--blue-hi));
  background-size: 220% 100%;
  background-position: 0% 50%;
  border: 0;
  color: #fff;
  box-shadow: 0 8px 22px rgba(106, 92, 240, 0.38);
  transition: background-position 0.5s ease;
}
/* Keep the gradient on hover (beat the generic .btn:hover) and animate it. */
.btn.big-blue:hover {
  background-image: linear-gradient(120deg, var(--blue-hi), var(--blue-lo), var(--blue-hi));
  background-size: 220% 100%;
  animation: big-blue-sweep 2.6s ease-in-out infinite;
}
@keyframes big-blue-sweep {
  0%, 100% { background-position: 0% 50%; }
  50% { background-position: 100% 50%; }
}
/* Secondary buttons: outline only, primary-colored text. */
.btn.secondary {
  width: 100%;
  background: transparent;
  border: 1.5px solid var(--blue);
  color: var(--blue);
  font-weight: 700;
  font-size: 15px;
  padding: 13px;
  border-radius: 13px;
  margin-top: 12px;
  transition: background 0.2s ease;
}
.btn.secondary:hover { background: rgba(90, 73, 214, 0.08); color: var(--blue); }
/* First secondary sits under the primary button; give it a touch more room. */
.btn.big-blue + .btn.secondary { margin-top: 16px; }

/* Full-screen like the menu: transparent over the page gradient, with the header
   spanning the whole width so back/burger sit at the true screen corners. The
   board, lives and toolbar center themselves within it. */
.app {
  min-height: 100vh;
  min-height: 100dvh; /* dvh tracks the visible viewport so mobile browser chrome
                         does not cut off the bottom toolbar (e.g. Firefox) */
  display: flex;
  flex-direction: column;
  padding: 8px 14px calc(16px + env(safe-area-inset-bottom, 0px));
}

/* --- Header --- */
.topbar {
  display: grid;
  grid-template-columns: 40px 1fr 40px;
  align-items: center;
  padding: 6px 2px 10px;
}
.titlewrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  line-height: 1.1;
}
.title {
  margin: 0;
  text-align: center;
  font-size: 20px;
  font-weight: 700;
  color: var(--title);
}
/* Subtitle line: lives . timer */
.substat {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  margin-top: 3px;
}
.substat-dot { color: #9aa4b0; font-weight: 700; }
.timer {
  font-size: 16px;
  font-weight: 600;
  color: #9aa4b0;
  letter-spacing: 0.3px;
  font-variant-numeric: tabular-nums;
}
.icon-btn {
  width: 40px;
  height: 40px;
  border: 0;
  background: transparent;
  color: var(--blue);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 10px;
}
.icon-btn:hover { background: #f0f3f8; }
.icon-btn svg { width: 26px; height: 26px; }

/* --- Lives (inline in the subtitle) --- */
.lives-inline { display: inline-flex; align-items: center; gap: 3px; }
.lives-inline svg { width: 16px; height: 16px; }
.heart-full { color: var(--heart); }
.heart-lost { color: var(--heart-lost); }

/* --- Board --- */
.stage {
  display: flex;
  justify-content: center;
  margin-top: 14px; /* sit under the header, not pushed to the screen bottom */
}
.board {
  position: relative; /* dividers are positioned against the board */
  display: grid;
  grid-template-columns: var(--rowclue) repeat(var(--cols), var(--cell));
  grid-template-rows: var(--colclue) repeat(var(--rows), var(--cell));
  /* pinch-zoom lets two fingers zoom the board (solvable on small screens),
     while single-finger drags still reach our paint handlers. */
  touch-action: pinch-zoom;
  align-content: start;
}
/* Dashed 1px group dividers sitting in the gap; they don't affect cell spacing.
   The dashes are painted with a repeating gradient so a 1px line can be dashed. */
.vdiv, .hdiv { position: absolute; pointer-events: none; }
.vdiv {
  top: var(--colclue); bottom: 0; width: 1px; transform: translateX(-0.5px);
  background: repeating-linear-gradient(to bottom, var(--sep) 0 4px, transparent 4px 8px);
}
.hdiv {
  left: var(--rowclue); right: 0; height: 1px; transform: translateY(-0.5px);
  background: repeating-linear-gradient(to right, var(--sep) 0 4px, transparent 4px 8px);
}
.board.hint-arming .cell { cursor: help; }
/* Brightness fade-in when a board (re)renders. */
.board.fade-in { animation: board-in 0.4s ease; }
@keyframes board-in {
  from { opacity: 0; filter: brightness(1.7); }
  to { opacity: 1; filter: brightness(1); }
}
.corner { grid-row: 1; grid-column: 1; }

/* Row/column clue numbers (no background). */
.colclue {
  grid-row: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  gap: 1px;
  padding-bottom: 6px;
  color: var(--clue-ink);
  font-size: var(--clue-font);
  font-weight: 700;
  line-height: 1.05;
  font-variant-numeric: tabular-nums;
}
.rowclue {
  grid-column: 1;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 6px;
  padding: 0 10px 0 0;
  color: var(--clue-ink);
  font-size: var(--clue-font);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.colclue.done, .rowclue.done { color: var(--clue-done); }

/* Cells are rounded chips (like the app icon). A transparent border makes the
   gap; the chip is inset via background-clip, so nothing shifts alignment. */
.cell {
  position: relative;
  border: var(--cell-gap) solid transparent;
  background: var(--cell-empty);
  background-clip: padding-box;
  border-radius: 26%;
  cursor: pointer;
  transition: box-shadow 0.25s ease; /* fade the hover row/column highlight */
}
/* Filled cells use the brand accent. */
.cell.filled {
  background: var(--blue);
  background-clip: padding-box;
}
/* Faint 5-block separators, painted into the gap on the block's edge cells. */
.cell.sep-r { border-right-color: var(--sep); }
.cell.sep-b { border-bottom-color: var(--sep); }

/* Cross drawn as two bars -- no font glyph needed. */
.cell.crossed::before,
.cell.crossed::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 16%;
  width: 2px;
  height: 68%;
  margin-left: -1px;
  background: var(--cross);
  border-radius: 1px;
}
.cell.crossed::before { transform: rotate(45deg); }
.cell.crossed::after { transform: rotate(-45deg); }

/* A misclick leaves a red cross so the mistake stays visible. */
.cell.mistake::before,
.cell.mistake::after { background: #e5484d; }

.cell.mistake { animation: flash 0.5s ease-out; }
@keyframes flash {
  0% { box-shadow: inset 0 0 0 100px rgba(231, 76, 60, 0.5); }
  100% { box-shadow: inset 0 0 0 100px rgba(231, 76, 60, 0); }
}

.cell.hintpop { animation: pop 0.4s ease-out; }
@keyframes pop {
  0% { transform: scale(0.4); }
  60% { transform: scale(1.15); }
  100% { transform: scale(1); }
}

/* --- Bottom toolbar --- */
.toolbar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 26px;
  padding: 22px 0 6px;
}
.toolbar.hidden, .endbar.hidden { display: none; }

/* --- End-of-game bar (condensed result + actions, replaces the toolbar) --- */
.endbar {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 14px 8px 6px;
  text-align: center;
}
.endbar-emoji { font-size: 30px; line-height: 1; }
.endbar-title { font-size: 20px; font-weight: 800; color: var(--title); }
.endbar-lines {
  color: #7c8695;
  font-weight: 600;
  font-size: 13px;
  line-height: 1.35;
  font-variant-numeric: tabular-nums;
}
.endbar-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  margin-top: 4px;
}
.endbar-actions .btn { padding: 9px 14px; font-size: 14px; }
.tool-toggle {
  position: relative;
  display: inline-flex;
  background: var(--panel);
  border-radius: 999px;
  padding: 5px;
  gap: 4px;
}
/* Sliding white "thumb" that animates between the two options. */
.tool-toggle::before {
  content: "";
  position: absolute;
  top: 5px;
  left: 5px;
  width: 62px;
  height: 46px;
  border-radius: 999px;
  background: #fff;
  box-shadow: 0 2px 6px rgba(30, 42, 56, 0.18);
  transition: transform 0.2s cubic-bezier(0.34, 1.3, 0.5, 1);
}
.tool-toggle[data-tool="cross"]::before { transform: translateX(0); }
.tool-toggle[data-tool="fill"]::before { transform: translateX(66px); }
.tool-opt {
  position: relative;
  z-index: 1;
  width: 62px;
  height: 46px;
  border: 0;
  background: transparent;
  border-radius: 999px;
  color: #7c8695;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color 0.2s ease;
}
.tool-opt svg { width: 24px; height: 24px; transition: transform 0.2s ease; }
.tool-opt.active { color: var(--ink); }
.tool-opt.active svg { animation: toolpop 0.25s ease; }
@keyframes toolpop {
  0% { transform: scale(0.7); }
  60% { transform: scale(1.15); }
  100% { transform: scale(1); }
}

.hint-btn {
  position: relative;
  width: 56px;
  height: 56px;
  border: 0;
  border-radius: 999px;
  background: var(--panel);
  color: var(--blue);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
.hint-btn svg { width: 28px; height: 28px; }
.hint-btn:disabled { color: #b7c0cd; cursor: default; }
/* Armed hint mode: highlight the button and gently pulse it. */
.hint-btn.armed { background: var(--blue); color: #fff; animation: hintpulse 1.1s ease-in-out infinite; }
.hint-btn.armed .hint-badge { background: #fff; color: var(--blue); }
@keyframes hintpulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(90, 73, 214, 0.45); }
  50% { box-shadow: 0 0 0 7px rgba(90, 73, 214, 0); }
}
.hint-badge {
  position: absolute;
  top: -2px;
  right: -2px;
  min-width: 20px;
  height: 20px;
  padding: 0 5px;
  border-radius: 999px;
  background: var(--blue);
  color: #fff;
  font-size: 12px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
}
.hint-btn:disabled .hint-badge { background: #b7c0cd; }

/* --- Sheets / overlays --- */
.sheet {
  position: fixed;
  inset: 0;
  background: rgba(20, 28, 40, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 20;
  padding: 16px;
}
.sheet.hidden { display: none; }
.sheet-card {
  background: #fff;
  border-radius: 16px;
  width: 100%;
  max-width: 420px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  padding: 18px 20px 22px;
}
.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}
.sheet-head h2 { margin: 0; font-size: 20px; color: var(--title); }
.close-btn {
  width: 34px; height: 34px; border: 0; background: transparent;
  color: #7c8695; cursor: pointer; border-radius: 8px;
}
.close-btn svg { width: 22px; height: 22px; }

.picker-card { max-width: 440px; }
.picker-body { max-height: 70vh; overflow-y: auto; }

.about-card { max-width: 460px; }
.about-body { max-height: 74vh; overflow-y: auto; font-size: 14.5px; line-height: 1.6; color: #4b5266; }
.about-body p { margin: 0 0 12px; }
.about-body h3 { margin: 18px 0 6px; font-size: 15px; color: var(--title); }
.about-body ul { margin: 0 0 12px; padding-left: 20px; }
.about-body li { margin: 0 0 7px; }
.about-body a { color: var(--blue); font-weight: 600; text-decoration: none; }
.about-body a:hover { text-decoration: underline; }
.about-credit { margin-top: 18px; color: #8a90a2; font-size: 13.5px; }

/* Visual guide: an auto-playing board (left) beside step cards (right) that
   highlight in sync. The board reuses the real .cell chips; it is purely
   visual, so it ignores pointer input. */
.about-guide {
  display: flex;
  align-items: flex-start;
  gap: 14px;
  margin: 6px 0 14px;
  padding: 14px;
  background: #f3f4fb;
  border-radius: 14px;
}
.about-demo-wrap { flex: 0 0 auto; }
.about-demo {
  --dcell: 24px;
  display: inline-grid;
  grid-template-columns: auto repeat(5, var(--dcell));
  grid-template-rows: auto repeat(5, var(--dcell));
  pointer-events: none;
}
.about-demo .dclue {
  display: flex; align-items: center; justify-content: center;
  font-size: 10px; font-weight: 700; color: var(--clue-ink);
  font-variant-numeric: tabular-nums; line-height: 1.05;
}
.about-demo .rowclue { flex-direction: row; gap: 3px; justify-content: flex-end; padding-right: 5px; }
.about-demo .colclue { flex-direction: column; justify-content: flex-end; padding-bottom: 2px; }
.about-demo-hearts {
  display: flex; justify-content: center; gap: 4px; margin-top: 10px;
}
.about-demo-hearts svg { width: 15px; height: 15px; }
.about-demo-hearts .heart-full { color: var(--heart); }
.about-demo-hearts .heart-lost { color: var(--heart-lost); }

.about-steps {
  flex: 1 1 auto; min-width: 0;
  margin: 0; padding: 0; list-style: none;
  display: flex; flex-direction: column; gap: 8px;
}
.about-step {
  display: flex; gap: 9px; align-items: flex-start;
  padding: 8px 10px; border-radius: 10px;
  background: #fff; border: 1px solid #e5e7f2;
  opacity: 0.55; transition: opacity 0.3s ease, border-color 0.3s ease, background 0.3s ease;
}
.about-step.active { opacity: 1; border-color: var(--blue); background: rgba(90, 73, 214, 0.06); }
.step-num {
  flex: 0 0 auto;
  width: 18px; height: 18px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 11px; font-weight: 800;
  background: #e5e7f2; color: #8a90a2;
}
.about-step.active .step-num { background: var(--blue); color: #fff; }
.step-body { display: flex; flex-direction: column; gap: 2px; }
.step-title { font-size: 12.5px; font-weight: 700; color: var(--title); }
.step-desc { font-size: 11.5px; line-height: 1.35; color: #6b7286; }

/* Inline glyphs that mirror the in-game controls, used in the rule list. */
.ib {
  display: inline-flex; align-items: center; justify-content: center;
  width: 1.15em; height: 1.15em; vertical-align: -0.22em; margin: 0 1px;
}
.ib svg { width: 100%; height: 100%; }
.ib-fill { color: var(--blue); }
.ib-cross { color: var(--cross); }
.ib-heart { color: var(--heart); }
.ib-hint { color: var(--blue); }

/* On very narrow screens the guide stacks: board on top, cards below. */
@media (max-width: 430px) {
  .about-guide { flex-direction: column; align-items: center; }
  .about-steps { width: 100%; }
}

/* On phones the picker/menu/about read better as full-screen sheets than as
   floating cards. */
@media (max-width: 560px) {
  #picker, #menu, #about { padding: 0; }
  #picker .sheet-card,
  #menu .sheet-card,
  #about .sheet-card {
    max-width: none;
    width: 100%;
    height: 100%;
    border-radius: 0;
    display: flex;
    flex-direction: column;
    padding: 16px 18px calc(16px + env(safe-area-inset-bottom, 0px));
  }
  #picker .picker-body,
  #about .about-body { max-height: none; flex: 1; }
}
.picker-group-title {
  font-size: 13px; font-weight: 700; color: #7c8695;
  text-transform: uppercase; letter-spacing: 0.5px;
  margin: 14px 2px 8px;
}
.picker-group-title:first-child { margin-top: 2px; }
.picker-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(54px, 1fr));
  gap: 10px 8px;
}
.picker-cell {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1px;
  border: 1px solid #dbe1ea;
  background: #f6f8fb;
  border-radius: 10px;
  min-height: 48px;
  padding: 4px 2px;
  font-weight: 700;
  color: var(--ink);
  cursor: pointer;
}
.picker-cell .pc-num { font-size: 15px; line-height: 1; }
.picker-cell .pc-time {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  font-size: 10px;
  font-weight: 600;
  color: #7c8695;
  line-height: 1;
}
.picker-cell .pc-time svg { width: 10px; height: 10px; color: #e0a92a; }
.picker-cell:hover { background: #eef2f8; }
.picker-cell.solved { background: #eefaf1; border-color: #bfe6cd; }
.picker-cell.current { background: var(--blue); border-color: var(--blue); color: #fff; }
.picker-cell.current .pc-time { color: #d9e6ff; }
.picker-cell.current .pc-time svg { color: #ffe08a; }
.pc-check {
  position: absolute;
  top: -6px;
  right: -6px;
  width: 18px;
  height: 18px;
  border-radius: 999px;
  background: #2fbf6b;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25);
}
.pc-check svg { width: 12px; height: 12px; }

.menu-danger { color: #c0392b; }

.menu-item {
  display: block;
  width: 100%;
  text-align: left;
  border: 0;
  background: #f6f8fb;
  border-radius: 10px;
  padding: 14px 16px;
  font-size: 16px;
  font-weight: 600;
  color: var(--ink);
  cursor: pointer;
  margin-bottom: 8px;
}
.menu-item:hover { background: #eef2f8; }
.menu-help { margin-top: 8px; color: #63707f; font-size: 13px; line-height: 1.5; }
.menu-help p { margin: 6px 0 0; }

.result-card { position: relative; text-align: center; max-width: 340px; }
.card-close { position: absolute; top: 6px; right: 6px; }
.result-emoji { font-size: 46px; }
.result-card h2 { margin: 6px 0 2px; font-size: 26px; color: var(--title); }
.result-name { margin: 0 0 2px; color: #7c8695; font-weight: 600; }
.result-stats { margin: 0 0 18px; color: #9aa4b0; font-weight: 600; font-size: 14px; font-variant-numeric: tabular-nums; }
/* Completed (review) modal: both lines share one size. */
.result-line { margin: 0 0 3px; color: #7c8695; font-weight: 600; font-size: 15px; font-variant-numeric: tabular-nums; }
.result-line-last { margin-bottom: 18px; }
.result-actions { display: flex; gap: 10px; justify-content: center; margin-top: 14px; }
.result-share {
  margin-top: 10px;
  width: 100%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}
.result-share svg { width: 18px; height: 18px; }

.btn {
  font-size: 15px;
  padding: 11px 16px;
  border: 1px solid #cfd6df;
  border-radius: 10px;
  background: #fff;
  color: var(--ink);
  font-weight: 600;
  cursor: pointer;
}
.btn:hover { background: #f2f5f9; }
.btn.primary { background: var(--blue); border-color: var(--blue); color: #fff; }
.btn.primary:hover { background: #2565d6; }
