:root {
  /* Text colours meet WCAG 4.5:1 on --bg and --panel. --accent is for outlines and highlights;
     --accent-fill is the orange behind white text, --accent-text is orange used as text. */
  --bg: #f6f1e7; --panel: #fffaf0; --ink: #2b2622; --muted: #766b62; --line: #d9cfc0;
  --yellow: #fff0c9; --yellow-edge: #ecc25e; --blue: #e3e6fb; --blue-edge: #9aa3e0;
  --accent: #e0703a; --accent-fill: #b85c30; --accent-text: #ac562d;
  --bad: #e5484d; --dot: #7a4fd6; --good: #257d55;
  --tap: 44px; /* smallest button height: Apple 44 pt; Android asks 48 dp */
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1d1a17; --panel: #27231f; --ink: #f3ece3; --muted: #a99d91; --line: #3d3731;
    --yellow: #4a3d1c; --yellow-edge: #a8862f; --blue: #272c4d; --blue-edge: #5c66b0;
    --dot: #b693ff; --accent-text: #e0703a; --good: #2f9e6b;
  }
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--ink); font-family: system-ui, -apple-system, "Segoe UI", sans-serif; }
button { font: inherit; color: inherit; }
#app { max-width: 520px; margin: 0 auto; padding: 16px; }

h1 { font-size: 26px; margin: 8px 0 4px; }
h1 small { font-size: 13px; color: var(--muted); font-weight: 500; }
.lead { color: var(--muted); margin: 0 0 16px; line-height: 1.4; }
.foot { color: var(--muted); font-size: 12px; margin-top: 20px; }

#tabs { display: flex; gap: 6px; flex-wrap: wrap; }
.tab { min-height: var(--tap); border: 1px solid var(--line); background: var(--panel); padding: 8px 14px; border-radius: 999px; font-size: 14px; cursor: pointer; }
.tab.on { background: var(--ink); color: var(--bg); border-color: var(--ink); }
#packBlurb { color: var(--muted); font-size: 14px; min-height: 2.6em; line-height: 1.4; }
#levels { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.lvl { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 14px 8px; text-align: center; cursor: pointer; }
.lvl b { display: block; font-size: 20px; margin-bottom: 2px; }
.lvl span { display: block; font-size: 12px; color: var(--muted); }
.lvl.done { border-color: var(--accent); }
.lvl .best { color: var(--accent-text); font-weight: 600; }

.topbar { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
#lvlTitle { font-weight: 700; text-align: center; font-size: 15px; }
#timer { font-variant-numeric: tabular-nums; color: var(--muted); min-width: 52px; text-align: right; }
.ghost { min-height: var(--tap); white-space: nowrap; background: transparent; border: 1px solid var(--line); border-radius: 10px; padding: 6px 12px; cursor: pointer; }

#board { display: grid; justify-content: center; margin: 12px auto 6px; touch-action: manipulation; }
.cell { position: relative; width: var(--cell); height: var(--cell); border-radius: 12px; border: 2px solid var(--line);
  background: var(--panel); padding: 0; cursor: pointer; display: flex; align-items: center; justify-content: center; }
.cell.yellow { background: var(--yellow); border-color: var(--yellow-edge); }
.cell.blue { background: var(--blue); border-color: var(--blue-edge); }
.cell.given { border-width: 3px; border-color: var(--ink); }
.cell.same { box-shadow: inset 0 0 0 3px color-mix(in srgb, var(--accent) 45%, transparent); }
.cell.sel { outline: 3px solid var(--accent); outline-offset: 2px; }
.cell.bad { box-shadow: 0 0 0 3px var(--bad), 0 0 14px color-mix(in srgb, var(--bad) 70%, transparent); }
.cell.flash { animation: flash 0.8s ease 3; }
@keyframes flash { 50% { transform: scale(1.08); box-shadow: 0 0 0 4px var(--accent), 0 0 18px var(--accent); } }
.cell .val { font-size: calc(var(--cell) * 0.5); font-weight: 600; line-height: 1; color: color-mix(in srgb, var(--ink) 75%, transparent); }
.cell.given .val { font-weight: 800; color: var(--ink); }
.notes { position: absolute; inset: 8% 8%; display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(2, 1fr);
  font-size: calc(var(--cell) * 0.22); font-weight: 600; color: var(--muted); place-items: center; pointer-events: none; }

.link { display: flex; align-items: center; justify-content: center; user-select: none; color: var(--ink); overflow: visible; }
.link svg { overflow: visible; flex: none; }
.link polyline { fill: none; stroke: currentColor; stroke-width: 4.5; stroke-linecap: round; stroke-linejoin: round; }
.link .dot { width: 13px; height: 13px; border-radius: 50%; background: var(--dot); }
.link.bad { color: var(--bad); }
.link.bad .dot { background: var(--bad); }

.lab { display: inline-flex; align-items: center; justify-content: center; min-width: 20px; height: 20px; padding: 0 5px; border-radius: 6px;
  background: var(--accent-fill); color: #fff; font-size: 12px; font-weight: 800; vertical-align: 1px; }
.lab.tgt { background: var(--ink); color: var(--bg); }
.cell .badge { position: absolute; top: -9px; right: -9px; z-index: 2; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25); }
.cell.target { outline: 3px dashed var(--ink); outline-offset: 3px; }
.cell.ref { box-shadow: inset 0 0 0 3px color-mix(in srgb, var(--accent) 55%, transparent); }

#hintBox { background: var(--panel); border: 2px solid var(--accent); border-radius: 14px; padding: 10px 14px 12px; margin: 6px 0 12px; font-size: 14px; line-height: 1.55; }
.hint-head { font-weight: 700; }
#hintSteps { margin: 8px 0 4px; padding-left: 22px; }
#hintSteps li { margin-bottom: 6px; }
.hint-key { color: var(--muted); font-size: 12px; margin: 4px 0 8px; }
#hintBox .row { display: flex; gap: 8px; }
#hintBox button { min-height: var(--tap); border-radius: 10px; padding: 7px 14px; cursor: pointer; border: 1px solid var(--line); }
#hintFill { background: var(--accent-fill); color: #fff; border-color: var(--accent-fill) !important; }

#msg { min-height: 1.5em; text-align: center; font-size: 14px; margin: 4px 0; color: var(--muted); }
#msg.good { color: var(--good); }

#palette { display: flex; justify-content: center; gap: 8px; margin: 6px 0 10px; }
.pal { flex: 1 1 0; min-width: 0; max-width: 54px; height: 58px; border-radius: 14px; border: 2px solid var(--line); background: var(--panel); font-size: 26px; font-weight: 700; cursor: pointer; }
.pal.yellow { background: var(--yellow); border-color: var(--yellow-edge); }
.pal.blue { background: var(--blue); border-color: var(--blue-edge); }
.notes-on .pal { border-style: dashed; }

.tools { display: flex; gap: 6px; justify-content: center; flex-wrap: wrap; }
.tools button { min-height: var(--tap); border: 1px solid var(--line); background: var(--panel); border-radius: 10px; padding: 8px 12px; cursor: pointer; font-size: 14px; }
.tools button.on { background: var(--accent-fill); color: #fff; border-color: var(--accent-fill); }

.rules { margin: 14px 0 6px; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 8px 12px; font-size: 14px; }
.rules summary { cursor: pointer; font-weight: 600; }
.rules ul { margin: 8px 0 2px; padding-left: 18px; line-height: 1.5; }
.rules .chip { display: inline-block; width: 14px; height: 14px; border-radius: 4px; border: 2px solid; vertical-align: -2px; }
.rules .chip.yellow { background: var(--yellow); border-color: var(--yellow-edge); }
.rules .chip.blue { background: var(--blue); border-color: var(--blue-edge); }
.rules .dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; background: var(--dot); vertical-align: middle; }

.toggles { display: flex; gap: 14px; align-items: center; flex-wrap: wrap; font-size: 13px; color: var(--muted); margin-top: 8px; }
.toggles label { display: inline-flex; align-items: center; gap: 8px; min-height: var(--tap); cursor: pointer; }
.toggles input { width: 22px; height: 22px; margin: 0; accent-color: var(--accent-fill); }
.toggles .ghost { margin-left: auto; font-size: 13px; }

#win { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.45); display: flex; align-items: center; justify-content: center; padding: 16px; }
#win[hidden] { display: none; }
#win .card { background: var(--panel); border-radius: 18px; padding: 24px; text-align: center; max-width: 320px; width: 100%; }
#win .big { font-size: 48px; }
#win h2 { margin: 6px 0; }
#win .row { display: flex; gap: 8px; justify-content: center; margin-top: 12px; }
#win button { min-height: var(--tap); border-radius: 10px; padding: 9px 14px; cursor: pointer; border: 1px solid var(--line); }
#nextBtn { background: var(--accent-fill); color: #fff; border-color: var(--accent-fill) !important; }
