/* ============ Bugdoku — garden theme ============ */
:root {
  --bg: #fbf4e4;
  --bg2: #f4e8c9;
  --ink: #3a2e22;
  --ink-soft: #7a6a56;
  --card: #fffaf0;
  --line: #3a2e22;
  --accent: #e0483c;       /* ladybug red */
  --accent-ink: #fff;
  --good: #3fa866;
  --bad: #e0483c;
  --shadow: 0 10px 30px -12px rgba(80, 50, 10, .35);
  --cell-gloss: rgba(255, 255, 255, .35);
  --font: ui-rounded, "SF Pro Rounded", "Nunito", "Avenir Next Rounded", "Trebuchet MS", system-ui, sans-serif;

  /* region colors */
  --c0: #f7b2ad; --c1: #ffd596; --c2: #fbec8f; --c3: #c5e59b;
  --c4: #97dcbc; --c5: #98d2f2; --c6: #b3b6f7; --c7: #dbb2f0;
  --c8: #f6b0d6; --c9: #d9c7a3; --c10: #a9c9bd; --c11: #f1a584;
}
:root[data-theme="dark"] {
  --bg: #1d2a22;
  --bg2: #16201a;
  --ink: #f4ecd8;
  --ink-soft: #a8b5a6;
  --card: #26352b;
  --line: #0e1510;
  --shadow: 0 12px 34px -12px rgba(0, 0, 0, .7);
  --cell-gloss: rgba(255, 255, 255, .08);
  --c0: #c9736d; --c1: #d4a05a; --c2: #c9b84e; --c3: #86b25a;
  --c4: #4fa482; --c5: #5a9dc4; --c6: #7a7fc9; --c7: #a276c0;
  --c8: #c4709f; --c9: #a39270; --c10: #6f9a8b; --c11: #c47a56;
}
@media (prefers-color-scheme: dark) {
  :root[data-theme="auto"] {
    --bg: #1d2a22; --bg2: #16201a; --ink: #f4ecd8; --ink-soft: #a8b5a6; --card: #26352b; --line: #0e1510;
    --shadow: 0 12px 34px -12px rgba(0, 0, 0, .7); --cell-gloss: rgba(255, 255, 255, .08);
    --c0: #c9736d; --c1: #d4a05a; --c2: #c9b84e; --c3: #86b25a;
    --c4: #4fa482; --c5: #5a9dc4; --c6: #7a7fc9; --c7: #a276c0;
    --c8: #c4709f; --c9: #a39270; --c10: #6f9a8b; --c11: #c47a56;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; min-height: 100%; }
body {
  font-family: var(--font);
  color: var(--ink);
  background:
    radial-gradient(1200px 600px at 50% -10%, var(--bg2), transparent 70%),
    var(--bg);
  min-height: 100dvh;
  overflow-x: hidden;
  -webkit-font-smoothing: antialiased;
  user-select: none;
  -webkit-user-select: none;
}
button { font: inherit; color: inherit; cursor: pointer; }
[hidden] { display: none !important; }

/* floating garden decorations */
.bg-deco span {
  position: fixed; left: var(--x); top: var(--y);
  font-size: var(--s); transform: rotate(var(--r));
  opacity: .5; pointer-events: none; filter: saturate(.9);
  animation: sway 6s ease-in-out infinite alternate;
}
.bg-deco span:nth-child(2n) { animation-duration: 8s; }
@keyframes sway { to { transform: rotate(calc(var(--r) * -1)) translateY(-6px); } }
@media (max-width: 560px) { .bg-deco { display: none; } }

.app {
  position: relative;
  max-width: 640px; margin: 0 auto;
  padding: max(14px, env(safe-area-inset-top)) 16px max(18px, env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 12px;
  min-height: 100dvh;
}

/* ---- header ---- */
.top { display: flex; align-items: center; justify-content: space-between; }
.brand { display: flex; align-items: center; gap: 10px; }
.logo {
  font-size: 2rem; line-height: 1;
  width: 48px; height: 48px; display: grid; place-items: center;
  background: var(--card); border-radius: 16px; box-shadow: var(--shadow);
  animation: wiggle 5s ease-in-out infinite;
}
@keyframes wiggle { 0%, 90%, 100% { transform: rotate(0); } 93% { transform: rotate(-12deg); } 96% { transform: rotate(10deg); } }
h1 { margin: 0; font-size: 1.7rem; letter-spacing: -.02em; line-height: 1; font-weight: 900; }
.tag { margin: 3px 0 0; font-size: .78rem; color: var(--ink-soft); font-weight: 600; }
.icon-btns { display: flex; gap: 6px; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 13px; border: 0;
  background: var(--card); box-shadow: var(--shadow); font-size: 1.1rem;
  transition: transform .12s;
}
.icon-btn:active { transform: scale(.92); }

/* ---- mode tabs ---- */
.modes {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px;
  background: color-mix(in srgb, var(--ink) 8%, transparent);
  padding: 4px; border-radius: 18px;
}
.modes button {
  border: 0; background: transparent; border-radius: 14px; padding: 7px 4px;
  display: flex; flex-direction: column; align-items: center; line-height: 1.15;
  transition: background .2s, transform .12s, box-shadow .2s;
}
.modes button b { font-size: .95rem; font-weight: 800; }
.modes button small { font-size: .7rem; color: var(--ink-soft); font-weight: 700; }
.modes button[aria-selected="true"] { background: var(--card); box-shadow: 0 4px 12px -4px rgba(80, 50, 10, .35); }
.modes button:active { transform: scale(.96); }

/* ---- status chips ---- */
.status { display: flex; align-items: center; justify-content: center; gap: 8px; flex-wrap: wrap; }
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 7px 13px; border-radius: 999px; border: 0;
  background: var(--card); box-shadow: var(--shadow);
  font-weight: 800; font-size: .92rem; font-variant-numeric: tabular-nums;
}
.chip-btn { font-weight: 700; }
.chip-btn:active { transform: scale(.95); }
.chip select {
  font: inherit; font-weight: 800; color: inherit; background: transparent; border: 0; outline: 0; cursor: pointer;
}
.chip label { color: var(--ink-soft); font-size: .8rem; }

/* ---- board ---- */
.stage { display: flex; flex-direction: column; align-items: center; gap: 12px; flex: 1; justify-content: center; }
.board-wrap {
  position: relative;
  width: min(100%, 560px, calc(100dvh - 330px));
  min-width: 260px;
  aspect-ratio: 1;
}
.board {
  --n: 8;
  width: 100%; height: 100%;
  display: grid;
  grid-template-columns: repeat(var(--n), 1fr);
  grid-template-rows: repeat(var(--n), 1fr);
  border-radius: 18px;
  overflow: hidden;
  border: 4px solid var(--line);
  background: var(--line);
  box-shadow: var(--shadow), 0 0 0 6px color-mix(in srgb, var(--card) 70%, transparent);
  touch-action: none;
  outline: none;
  container-type: size;
  --thin: color-mix(in srgb, var(--line) 28%, transparent);
  animation: board-in .45s cubic-bezier(.2, 1.2, .4, 1);
}
@keyframes board-in { from { transform: scale(.94) rotate(-1deg); opacity: 0; } }
.board:focus-visible { box-shadow: var(--shadow), 0 0 0 4px var(--accent); }

.cell {
  position: relative;
  background: var(--rc);
  display: grid; place-items: center;
  font-size: calc(100cqw / var(--n) * .6);
  cursor: pointer;
  /* borders (thick where the region changes, hairline otherwise) are set inline by app.js */
  transition: filter .15s;
}
.cell::before { /* soft gloss */
  content: ""; position: absolute; inset: 0; pointer-events: none;
  background: linear-gradient(160deg, var(--cell-gloss), transparent 55%);
}
.cell:hover { filter: brightness(1.06); }

.cell .glyph { position: relative; line-height: 1; z-index: 1; }
/* The ✕ marker: a chunky, slightly hand-inked cross with round ends (an SVG mask, so it takes the theme's ink colour)
   plus a soft light edge underneath so it looks pressed into the leaf. */
.cell.mark .glyph::before, .cell.auto .glyph::before {
  content: ""; display: block;
  width: .98em; height: .98em;                      /* about 58% of the square */
  background: var(--x-ink, var(--line)); opacity: .78;
  -webkit-mask: var(--x-shape) center / contain no-repeat; mask: var(--x-shape) center / contain no-repeat;
  filter: drop-shadow(0 1.5px 0 rgba(255, 255, 255, .55));
}
.cell.auto .glyph::before { opacity: .24; width: .8em; height: .8em; filter: none; }
.cell.mark .glyph { animation: pop-x .26s cubic-bezier(.3, 1.7, .5, 1); }
@keyframes pop-x { from { transform: scale(1.7) rotate(-24deg); opacity: 0; } 60% { opacity: 1; } }
:root {
  --x-shape: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cg fill='none' stroke='%23000' stroke-width='4.5' stroke-linecap='round'%3E%3Cpath d='M5.2 5.7Q12.6 11.5 18.9 18.5'/%3E%3Cpath d='M18.7 5.3Q11.4 12.5 5.4 18.8'/%3E%3C/g%3E%3C/svg%3E");
}

.cell.bug .glyph { animation: drop .35s cubic-bezier(.3, 1.6, .5, 1); filter: drop-shadow(0 3px 2px rgba(0, 0, 0, .3)); }
@keyframes drop { from { transform: translateY(-60%) scale(1.5); opacity: 0; } }

.reg-icon { display: none; position: absolute; right: 6%; bottom: 4%; width: 30%; height: 30%; opacity: .6; pointer-events: none; z-index: 1; }
.reg-icon .bug-svg { width: 100%; height: 100%; }
.board.show-icons .cell:not(.bug) .reg-icon { display: block; }

/* ---- bug artwork (SVG sprite, see js/bugart.js) ---- */
.bug-svg { display: block; width: 1em; height: 1em; overflow: visible; flex: none; }
.cell .glyph .bug-svg { width: 1.45em; height: 1.45em; }
.chip-bug .bug-svg { width: 1.15em; height: 1.15em; }
.chip-bug { display: inline-flex; }
.win-bug .bug-svg { width: 1em; height: 1em; filter: drop-shadow(0 6px 4px rgba(0, 0, 0, .3)); }
.logo .bug-svg { width: 1em; height: 1em; filter: drop-shadow(0 2px 1px rgba(0, 0, 0, .3)); }

.cell.conflict {
  background:
    repeating-linear-gradient(45deg, rgba(224, 72, 60, .55) 0 6px, rgba(224, 72, 60, .25) 6px 12px),
    var(--rc);
}
.cell.conflict .glyph { animation: shake .35s; }
@keyframes shake { 25% { transform: translateX(-12%); } 75% { transform: translateX(12%); } }

.cell.focus { box-shadow: inset 0 0 0 3px var(--accent); z-index: 2; }
.cell.hinted { animation: hint-pulse 1s ease-in-out 3; z-index: 2; }
@keyframes hint-pulse { 50% { box-shadow: inset 0 0 0 5px #fff, inset 0 0 0 9px var(--accent); transform: scale(1.04); } }
.cell.celebrate { animation: cheer .6s ease-in-out both; }
@keyframes cheer { 50% { transform: scale(1.12) rotate(6deg); } }

.board.solved .cell { cursor: default; }

.pause-cover, .loading {
  position: absolute; inset: 0; border-radius: 18px; z-index: 5;
  display: grid; place-items: center; text-align: center;
  background: color-mix(in srgb, var(--card) 94%, transparent);
  backdrop-filter: blur(8px);
  border: 4px solid var(--line);
}
.pause-cover .big, .loading .big { font-size: 4rem; }
.pause-cover p, .loading p { margin: 6px 0 14px; font-weight: 800; font-size: 1.2rem; }
.loading p { margin-bottom: 0; }
.bounce { animation: bounce .7s ease-in-out infinite alternate; }
@keyframes bounce { to { transform: translateY(-14px) rotate(10deg); } }

.rule-line {
  margin: 0; text-align: center; font-size: .86rem; color: var(--ink-soft);
  font-weight: 600; max-width: 430px; line-height: 1.4;
}
.rule-line b { color: var(--ink); }

/* ---- toolbar ---- */
.toolbar { display: flex; justify-content: center; gap: 8px; flex-wrap: wrap; }
.tool {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  min-width: 68px; padding: 8px 10px; border: 0; border-radius: 16px;
  background: var(--card); box-shadow: var(--shadow);
  font-size: .72rem; font-weight: 800; color: var(--ink-soft);
  transition: transform .12s, background .2s, color .2s;
}
.tool span { font-size: 1.25rem; line-height: 1.1; filter: saturate(1.1); }
.tool:active { transform: scale(.93); }
.tool:disabled { opacity: .4; cursor: default; }
.tool.toggle[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); }

/* ---- buttons / dialogs ---- */
.btn {
  border: 0; border-radius: 16px; padding: 12px 20px; font-weight: 800; font-size: 1rem;
  background: color-mix(in srgb, var(--ink) 10%, transparent); color: var(--ink);
  transition: transform .12s;
}
.btn.primary { background: var(--accent); color: var(--accent-ink); box-shadow: 0 6px 16px -6px var(--accent); }
.btn:active { transform: scale(.96); }
.link { background: none; border: 0; color: var(--ink-soft); font-weight: 700; margin-top: 8px; text-decoration: underline; }

dialog.card {
  border: 0; border-radius: 26px; padding: 24px 22px 20px;
  background: var(--card); color: var(--ink);
  width: min(420px, calc(100vw - 28px));
  max-height: calc(100dvh - 40px); overflow: auto;
  box-shadow: 0 30px 80px -20px rgba(0, 0, 0, .6);
  text-align: center;
  font-family: var(--font);
}
dialog.card.wide { width: min(520px, calc(100vw - 28px)); text-align: left; }
dialog.card[open] { animation: dlg-in .3s cubic-bezier(.2, 1.3, .4, 1); }
dialog.card::backdrop { background: rgba(30, 20, 10, .5); backdrop-filter: blur(3px); }
@keyframes dlg-in { from { transform: translateY(24px) scale(.94); opacity: 0; } }
dialog h2:focus { outline: none; }
dialog h2 { margin: 0 0 8px; font-size: 1.6rem; font-weight: 900; }
dialog h3 { margin: 16px 0 6px; font-size: 1rem; }
dialog .btn { width: 100%; margin-top: 10px; }
dialog .sub { margin: 0 0 14px; color: var(--ink-soft); font-weight: 600; }

.win-bug { font-size: 4.2rem; animation: bounce .6s ease-in-out infinite alternate; }
.stat-row { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 10px 0 8px; }
.stat-row div { background: color-mix(in srgb, var(--ink) 7%, transparent); border-radius: 16px; padding: 10px 4px; }
.stat-row b { display: block; font-size: 1.5rem; font-weight: 900; font-variant-numeric: tabular-nums; }
.stat-row small { color: var(--ink-soft); font-weight: 700; font-size: .72rem; }
.best { display: grid; gap: 6px; text-align: left; margin-bottom: 4px; }
.best div { display: flex; justify-content: space-between; background: color-mix(in srgb, var(--ink) 7%, transparent); border-radius: 12px; padding: 8px 14px; font-weight: 700; }

.rules { list-style: none; padding: 0; margin: 8px 0 12px; display: grid; gap: 8px; }
.rules li { display: flex; gap: 10px; align-items: center; line-height: 1.35; }
.rules .n {
  flex: none; width: 28px; height: 28px; border-radius: 50%; display: grid; place-items: center;
  background: var(--accent); color: #fff; font-weight: 900; font-size: .9rem;
}
.how { display: grid; gap: 6px; font-size: .92rem; background: color-mix(in srgb, var(--ink) 7%, transparent); padding: 12px 14px; border-radius: 16px; line-height: 1.4; }
.how .x { color: var(--accent); }
kbd {
  font: inherit; font-weight: 800; font-size: .8em; padding: 1px 7px; border-radius: 7px;
  background: var(--card); box-shadow: 0 2px 0 color-mix(in srgb, var(--ink) 25%, transparent);
}
.tips { margin: 0 0 6px; padding-left: 20px; display: grid; gap: 5px; font-size: .92rem; line-height: 1.4; }

dialog .row {
  display: flex; align-items: center; justify-content: space-between; gap: 12px; text-align: left;
  padding: 11px 2px; border-bottom: 1px solid color-mix(in srgb, var(--ink) 12%, transparent); font-weight: 800;
}
dialog .row small { display: block; font-weight: 600; color: var(--ink-soft); font-size: .76rem; margin-top: 1px; }
dialog input[type="checkbox"] {
  appearance: none; flex: none; width: 48px; height: 28px; border-radius: 99px; position: relative;
  background: color-mix(in srgb, var(--ink) 22%, transparent); transition: background .2s; cursor: pointer;
}
dialog input[type="checkbox"]::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%;
  background: #fff; box-shadow: 0 2px 4px rgba(0,0,0,.3); transition: transform .2s cubic-bezier(.3,1.5,.5,1);
}
dialog input[type="checkbox"]:checked { background: var(--good); }
dialog input[type="checkbox"]:checked::after { transform: translateX(20px); }
.seg { display: flex; background: color-mix(in srgb, var(--ink) 10%, transparent); border-radius: 12px; padding: 3px; }
.seg button { border: 0; background: transparent; padding: 6px 10px; border-radius: 9px; font-weight: 800; font-size: .82rem; }
.seg button[aria-pressed="true"] { background: var(--card); box-shadow: 0 2px 6px rgba(0,0,0,.2); }

/* ---- toast + confetti ---- */
.toast {
  position: fixed; left: 50%; bottom: max(26px, env(safe-area-inset-bottom)); z-index: 50;
  transform: translate(-50%, 30px); opacity: 0; pointer-events: none;
  background: var(--ink); color: var(--bg); padding: 10px 18px; border-radius: 99px;
  font-weight: 800; font-size: .9rem; max-width: calc(100vw - 32px); text-align: center;
  transition: transform .3s cubic-bezier(.2, 1.4, .4, 1), opacity .25s;
}
.toast.show { transform: translate(-50%, 0); opacity: 1; }
#confetti { position: fixed; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 100; }

@media (max-height: 700px) {
  .rule-line { display: none; }
  .app { gap: 8px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
}
