:root {
  color-scheme: light;
  background: #f8f4eb;
  color: #273833;
  font-family: "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
  font-size: 16px;
  --paper: #fffcf5;
  --line: #e9decf;
  --ink: #273833;
  --muted: #5d6b62;
}
* { box-sizing: border-box; }
html, body { margin: 0; width: 100%; height: 100%; overflow: hidden; overscroll-behavior: none; }
body {
  display: flex;
  background: radial-gradient(ellipse at 10% 0%, #fffaf0, #f8f4eb 62%);
}
button, select { font: inherit; }
button:not(:disabled), select { cursor: pointer; }
button:focus-visible, select:focus-visible { outline: 2px solid #82523b; outline-offset: 3px; }
[hidden] { display: none !important; }
.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip-path: inset(50%);
}

.game {
  width: min(100%, 1800px);
  height: 100vh;
  height: 100dvh;
  max-height: 872px;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: 10px;
  padding: 0 clamp(12px, 2.65vw, 42px) max(10px, env(safe-area-inset-bottom));
  margin: 0 auto;
}
.game, .game *, #controls-note {
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}
.masthead, .header-main, .brand, .stats, .cat-count, .star-meter {
  display: flex;
  align-items: center;
}
.masthead {
  min-width: 0;
  min-height: 57px;
  justify-content: space-between;
  gap: 12px;
  padding-top: env(safe-area-inset-top);
}
.header-main { flex: 1; min-width: 0; gap: clamp(12px, 3vw, 36px); }
.brand { gap: 9px; flex-shrink: 0; }
.brand h1 { margin: 0; font-size: clamp(1.13rem, 2vw, 1.64rem); font-weight: 770; letter-spacing: .045em; white-space: nowrap; }
.brand-mark {
  width: 36px;
  height: 36px;
  border-radius: 48% 52% 48% 52%;
  display: grid;
  place-items: center;
  background: #e8a07d;
  color: #5d3224;
  transform: rotate(-11deg);
  box-shadow: inset 0 -3px 0 #aa59462a, inset 0 1px 0 #ffffff7a;
}
.paw {
  display: block;
  position: relative;
  width: 13px;
  height: 10px;
  margin-top: 7px;
  background: currentColor;
  border-radius: 70% 70% 57% 57%;
  flex-shrink: 0;
}
.paw::before {
  content: "";
  position: absolute;
  top: -4px;
  left: 4px;
  width: 4px;
  height: 5px;
  border-radius: 50%;
  box-shadow: -7px -1px currentColor, -3px -5px currentColor, 3px -5px currentColor, 7px -1px currentColor;
}
.level-field { min-width: 0; position: relative; }
.level-field::after {
  content: "";
  position: absolute;
  pointer-events: none;
  right: 13px;
  top: 42%;
  width: 6px;
  height: 6px;
  border: solid #526457;
  border-width: 0 1.5px 1.5px 0;
  transform: rotate(45deg);
}
#level-picker {
  appearance: none;
  display: block;
  width: min(100%, 268px);
  height: 37px;
  border: 1px solid #e4d9c9;
  border-radius: 10px;
  background: var(--paper);
  color: var(--ink);
  padding: 0 29px 0 11px;
  font-size: .875rem;
  font-weight: 680;
  text-overflow: ellipsis;
}
.stats { flex-shrink: 0; gap: 8px; }
.cat-count, .star-meter {
  justify-content: center;
  gap: 5px;
  height: 36px;
  border: 1px solid #e7ddcc;
  border-radius: 11px;
  background: var(--paper);
}
.cat-count { padding: 0 10px; color: #46634e; font-weight: 750; font-size: .9rem; font-variant-numeric: tabular-nums; white-space: nowrap; }
.cat-count .paw { transform: scale(.66) rotate(-8deg); margin-right: 2px; }
.star-meter { padding: 0 9px; color: #a36a16; font: 1.12rem/1 system-ui, sans-serif; gap: 2px; }
.star-meter .spent { color: #7c6c5b; }

.stage {
  position: relative;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  overflow: clip;
  isolation: isolate;
  border-radius: 20px;
  background: #b8dbee url("海边小镇.jpg") center center / cover no-repeat;
  border: 1px solid #aabec483;
  box-shadow: 0 6px 22px #51422418;
  touch-action: none;
  cursor: grab;
}
.stage[data-dragging="true"], .stage[data-dragging="true"] .tile { cursor: grabbing; }
#scene, .falling-petals { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.tile-controls {
  position: absolute;
  top: 0;
  left: 0;
  display: grid;
  width: var(--map-span);
  height: var(--map-span);
  grid-template-columns: repeat(var(--board-size), minmax(0, 1fr));
  grid-template-rows: repeat(var(--board-size), minmax(0, 1fr));
  transform-origin: top left;
  pointer-events: none;
}
.tile {
  min-width: 0;
  min-height: 0;
  position: relative;
  border: 0;
  background: none;
  padding: 0;
  appearance: none;
  pointer-events: auto;
  -webkit-tap-highlight-color: transparent;
}
.tile[data-fixed="true"] { cursor: inherit; }
.tile:focus-visible { outline: none; }
.tap-switch {
  position: absolute;
  z-index: 1;
  width: calc(var(--map-span) / var(--board-size));
  height: calc(var(--map-span) / var(--board-size));
  margin: 0;
  opacity: .001;
  pointer-events: auto;
}
.stage.flat .tile {
  color: #48342c;
  margin: 1px;
  border-radius: 7px;
  font-size: 1.65rem;
  background: var(--tile-color);
  box-shadow: inset 0 2px #ffffff69, inset 0 -3px #4b392629;
}
.stage.flat[data-given-markers="true"] .tile[data-fixed="true"]::after {
  content: "";
  position: absolute;
  left: 10%;
  top: 10%;
  width: 12%;
  aspect-ratio: 1;
  border-radius: 50%;
  background: #f8de99;
  box-shadow: 0 0 0 1px #6c5a42;
}
.stage.flat .tile[data-excluded="true"] { color: #4f4138; font-weight: 760; }
.stage.flat .tile[data-hint="true"] { box-shadow: inset 0 0 0 3px #fffbdb, inset 0 0 16px #fffbdbc7; }
.stage.flat .tile:focus-visible { outline: 3px solid #734d34; outline-offset: -3px; z-index: 1; }

.map-tools {
  position: absolute;
  bottom: 12px;
  right: 12px;
  display: flex;
  gap: 3px;
  padding: 3px;
  border-radius: 12px;
  border: 1px solid #fcf7e9da;
  background: #fffcf6f2;
  box-shadow: 0 3px 12px #384c5226;
  cursor: default;
}
.map-tools button {
  width: 38px;
  height: 36px;
  display: grid;
  place-items: center;
  padding: 0;
  border: 0;
  border-radius: 8px;
  color: #35483f;
  background: transparent;
  font: 1.35rem/1 system-ui, sans-serif;
}
.map-tools button:hover:not(:disabled) { background: #edeadc; }
.map-tools .fit-map { width: 46px; font-size: .82rem; font-weight: 700; }
.map-tools button:disabled { opacity: .35; }
.map-tools button:focus-visible { outline-offset: -2px; }

.falling-petals { pointer-events: none; }
.falling-petals i {
  position: absolute;
  top: 23%;
  left: var(--x);
  width: var(--size);
  height: calc(var(--size) * .61);
  border-radius: 60% 30%;
  background: var(--fill);
  animation: float-away 2.25s ease-out var(--delay) both;
}
@keyframes float-away {
  0% { transform: translateY(8px) rotate(0); opacity: 0; }
  15% { opacity: 1; }
  50% { transform: translate(var(--dx), -45px) rotate(150deg); }
  100% { transform: translate(calc(var(--dx) * 1.8), 210px) rotate(350deg); opacity: 0; }
}

.game-footer { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-width: 0; }
.status { margin: 0; color: var(--muted); line-height: 1.45; font-size: .91rem; }
.status[data-kind="conflict"] { color: #934a35; }
.status[data-kind="win"] { color: #41604a; font-weight: 700; }
.actions { flex-shrink: 0; display: flex; gap: 7px; }
.actions button, .rules-dialog button {
  min-height: 40px;
  border: 1px solid #e2d7c8;
  border-radius: 9px;
  font-size: .875rem;
  font-weight: 680;
  padding: 0 13px;
  white-space: nowrap;
}
.hint-button { background: #e8edde; color: #354b3e; border-color: #d5dfcb !important; }
.hint-button:not(:disabled):hover { background: #d8e2cb; }
.reset-button, .rules-button { background: var(--paper); color: var(--ink); }
.reset-button:not(:disabled):hover, .rules-button:hover { background: #f4eddf; }
.actions button:disabled { opacity: .48; }
.next-button, .rules-dialog button { background: #53714f; color: #fffdf6; border-color: #718d66 !important; }
.next-button:hover, .rules-dialog button:hover { background: #43623f; }

.rules-dialog {
  width: min(calc(100% - 24px), 410px);
  max-height: min(440px, 92dvh);
  padding: 23px;
  border: 1px solid var(--line);
  border-radius: 20px;
  background: var(--paper);
  color: var(--ink);
  box-shadow: 0 17px 50px #25393040;
}
.rules-dialog::backdrop { background: #1b2e30a0; }
.rules-dialog h2 { margin: 0 0 11px; font-size: 1.25rem; }
.rules-dialog ul { margin: 0 0 17px; padding-inline-start: 1.22em; font-size: .95rem; line-height: 1.55; }
.rules-dialog li + li { margin-top: 7px; }

@media (max-width: 660px) {
  .game { gap: 7px; }
  .masthead { min-height: 49px; gap: 6px; }
  .header-main { gap: 7px; }
  .brand-mark { width: 32px; height: 32px; }
  .brand h1 { position: absolute; width: 1px; height: 1px; margin: -1px; overflow: hidden; clip-path: inset(50%); }
  #level-picker { height: 34px; font-size: .82rem; }
  .stats { gap: 5px; }
  .cat-count, .star-meter { height: 33px; }
  .cat-count { padding: 0 7px; font-size: .82rem; }
  .star-meter { padding: 0 6px; font-size: 1rem; }
  .stage { border-radius: 15px; }
  .game-footer { display: grid; gap: 6px; }
  .status { min-height: 2.64em; line-height: 1.32; font-size: .86rem; }
  .actions { justify-content: start; }
  .actions button { min-height: 37px; padding: 0 11px; }
  .map-tools { bottom: 8px; right: 8px; }
}
@media (max-width: 345px) {
  .game { padding-left: 8px; padding-right: 8px; }
  .brand-mark { width: 28px; height: 28px; }
  .cat-count .paw { display: none; }
  #level-picker { padding-left: 7px; }
}
@media (prefers-reduced-motion: reduce) {
  .falling-petals { display: none; }
}
