:root {
  --bg: #f7f1ea;
  --bg-soft: #fbf7f2;
  --surface: #ffffff;
  --text: #6b4f42;
  --text-soft: #a58978;
  --accent: #e8a05c;
  --accent-dark: #d4893f;
  --danger: #e86a5c;
  --success: #5cb88a;
  --shadow: 0 8px 28px rgba(107, 79, 66, 0.08);
  --radius: 20px;
  --radius-sm: 14px;
  --cell-gap: 6px;
  --font: "Nunito", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

body {
  min-height: 100dvh;
}

button {
  font-family: inherit;
  border: none;
  background: none;
  cursor: pointer;
  color: inherit;
}

#app {
  min-height: 100dvh;
  max-width: 480px;
  margin: 0 auto;
  position: relative;
  padding-top: var(--safe-t);
  padding-bottom: var(--safe-b);
}

.screen {
  display: none;
  min-height: 100dvh;
  padding: 16px 18px 28px;
  flex-direction: column;
}

.screen.active {
  display: flex;
}

/* —— Menu —— */
.menu-hero {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding-top: 40px;
}

.menu-cat {
  animation: float 3s ease-in-out infinite;
  filter: drop-shadow(0 12px 20px rgba(43, 43, 43, 0.15));
}

@keyframes float {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-8px); }
}

.logo {
  margin: 18px 0 6px;
  font-size: 2.6rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  color: var(--text);
}

.tagline {
  margin: 0;
  color: var(--text-soft);
  font-size: 0.95rem;
  font-weight: 600;
}

.menu-actions {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding-bottom: 12px;
}

.menu-progress {
  text-align: center;
  color: var(--text-soft);
  font-size: 0.85rem;
  font-weight: 600;
  min-height: 1.2em;
  margin: 4px 0 8px;
}

/* —— Buttons —— */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 48px;
  padding: 12px 22px;
  border-radius: 999px;
  font-weight: 700;
  font-size: 1rem;
  transition: transform 0.15s ease, opacity 0.15s ease, background 0.15s ease;
}

.btn:active {
  transform: scale(0.97);
}

.btn-primary {
  background: linear-gradient(180deg, #f0b06a, var(--accent));
  color: #fff;
  box-shadow: 0 8px 18px rgba(232, 160, 92, 0.35);
}

.btn-primary:hover {
  background: linear-gradient(180deg, #f2b878, var(--accent-dark));
}

.btn-ghost {
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}

.btn-lg {
  min-height: 54px;
  font-size: 1.08rem;
  width: 100%;
}

/* —— Top bar —— */
.top-bar,
.game-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 12px;
}

.top-bar h2 {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 800;
}

.icon-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--surface);
  box-shadow: var(--shadow);
  display: grid;
  place-items: center;
  color: var(--text);
  flex-shrink: 0;
}

.icon-btn:active {
  transform: scale(0.95);
}

.icon-btn.ghost {
  visibility: hidden;
  pointer-events: none;
}

/* —— Levels —— */
.levels-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
  padding: 8px 0 24px;
}

.level-btn {
  aspect-ratio: 1;
  border-radius: 18px;
  background: var(--surface);
  box-shadow: var(--shadow);
  font-weight: 800;
  font-size: 1.15rem;
  color: var(--text);
  position: relative;
  transition: transform 0.15s ease;
}

.level-btn:active {
  transform: scale(0.96);
}

.level-btn.cleared {
  background: linear-gradient(160deg, #fff7ef, #ffe8cc);
  color: var(--accent-dark);
}

.level-btn.cleared::after {
  content: "✓";
  position: absolute;
  top: 6px;
  right: 8px;
  font-size: 0.7rem;
  color: var(--success);
}

.level-btn.locked {
  opacity: 0.45;
  cursor: not-allowed;
}

.level-btn.current {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.level-btn .size-tag {
  display: block;
  font-size: 0.65rem;
  font-weight: 700;
  color: var(--text-soft);
  margin-top: 2px;
}

/* —— How to play —— */
.how-content {
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding-bottom: 24px;
}

.rule-card {
  display: flex;
  gap: 14px;
  align-items: center;
  background: var(--surface);
  border-radius: var(--radius);
  padding: 14px;
  box-shadow: var(--shadow);
}

.rule-card h3 {
  margin: 0 0 4px;
  font-size: 1rem;
}

.rule-card p {
  margin: 0;
  font-size: 0.88rem;
  color: var(--text-soft);
  line-height: 1.45;
  font-weight: 600;
}

.rule-mini {
  width: 64px;
  height: 64px;
  border-radius: 12px;
  background: #f3ebe3;
  flex-shrink: 0;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 2px;
  padding: 6px;
}

.rule-mini span {
  border-radius: 4px;
  font-size: 10px;
  display: grid;
  place-items: center;
  font-weight: 800;
  color: rgba(0, 0, 0, 0.25);
}

.how-tips {
  background: #fff8f0;
  border-radius: var(--radius);
  padding: 16px 18px;
  border: 1px dashed #ecc9a0;
}

.how-tips p {
  margin: 0 0 10px;
  font-size: 0.9rem;
  line-height: 1.5;
  color: var(--text);
  font-weight: 600;
}

.how-tips p:last-child {
  margin-bottom: 0;
}

/* —— Game header stats —— */
.stat-pair {
  display: flex;
  gap: 28px;
}

.stat {
  text-align: center;
}

.stat-label {
  display: block;
  font-size: 0.78rem;
  color: var(--text-soft);
  font-weight: 700;
}

.stat-value {
  display: block;
  font-size: 1.55rem;
  font-weight: 800;
  line-height: 1.15;
  color: var(--text);
}

.status-row {
  display: flex;
  justify-content: center;
  gap: 12px;
  margin: 4px 0 12px;
}

.pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--surface);
  border-radius: 999px;
  padding: 6px 14px 6px 8px;
  box-shadow: var(--shadow);
  font-weight: 800;
  font-size: 0.95rem;
  min-height: 40px;
}

.lives-pill {
  padding: 6px 12px;
  gap: 4px;
}

.fish {
  width: 22px;
  height: 22px;
  display: inline-block;
  transition: opacity 0.2s ease, transform 0.2s ease;
}

.fish.lost {
  opacity: 0.25;
  filter: grayscale(1);
  transform: scale(0.9);
}

/* —— Rules bar —— */
.rules-bar {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 4px;
  margin-bottom: 12px;
  scrollbar-width: none;
}

.rules-bar::-webkit-scrollbar {
  display: none;
}

.rules-bar.hidden {
  display: none;
}

.rule-chip {
  flex: 1 0 auto;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--surface);
  border-radius: 14px;
  padding: 8px 10px;
  box-shadow: var(--shadow);
  font-size: 0.68rem;
  font-weight: 700;
  color: var(--text-soft);
  white-space: nowrap;
}

.rule-icon {
  width: 28px;
  height: 28px;
  border-radius: 8px;
  background: #f4ebe3;
  flex-shrink: 0;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 1px;
  padding: 3px;
}

.rule-icon i {
  border-radius: 2px;
  font-style: normal;
  font-size: 7px;
  display: grid;
  place-items: center;
  font-weight: 800;
  color: rgba(0, 0, 0, 0.28);
}

/* —— Board —— */
.board-wrap {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
  padding: 4px 0 12px;
}

.board {
  --n: 4;
  display: grid;
  grid-template-columns: repeat(var(--n), 1fr);
  grid-template-rows: repeat(var(--n), 1fr);
  gap: var(--cell-gap);
  width: min(100%, 400px);
  aspect-ratio: 1;
  background: var(--surface);
  border-radius: 22px;
  padding: 12px;
  box-shadow: var(--shadow);
  user-select: none;
}

.cell {
  border-radius: calc(8px + (10 - var(--n)) * 1px);
  position: relative;
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: transform 0.12s ease, box-shadow 0.12s ease, filter 0.15s ease;
  border: 2px solid transparent;
  min-width: 0;
  min-height: 0;
}

.cell:active:not(.locked-pre) {
  transform: scale(0.94);
}

.cell.cat-placed {
  z-index: 1;
}

.cell.mark-x::after {
  content: "×";
  font-size: clamp(1.1rem, 6vw, 1.8rem);
  font-weight: 800;
  color: rgba(255, 255, 255, 0.92);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.15);
  line-height: 1;
}

.cell.invalid {
  animation: shake 0.4s ease;
  box-shadow: 0 0 0 3px var(--danger);
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-4px); }
  40% { transform: translateX(4px); }
  60% { transform: translateX(-3px); }
  80% { transform: translateX(3px); }
}

.cell.hint-glow {
  box-shadow: 0 0 0 3px #fff, 0 0 0 6px var(--accent);
  animation: pulse 0.8s ease 2;
}

@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.04); }
}

.cell.dimmed {
  filter: brightness(0.55);
}

.cat-face {
  width: 78%;
  height: 78%;
  max-width: 56px;
  max-height: 56px;
  object-fit: contain;
  object-position: center top;
  pointer-events: none;
  user-select: none;
  filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.14));
  animation: pop 0.28s cubic-bezier(0.2, 0.9, 0.3, 1.3);
}

.pill-cat img,
.cat-tool img {
  display: block;
  object-fit: contain;
  object-position: center top;
  filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.1));
}

.pill-cat img {
  width: 28px;
  height: 28px;
}

.cat-tool img {
  width: 36px;
  height: 36px;
}

@keyframes pop {
  from { transform: scale(0.4); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

/* —— Toolbar —— */
.toolbar {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 14px;
  padding: 8px 0 4px;
}

.tool-btn {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background: var(--surface);
  box-shadow: var(--shadow);
  position: relative;
  display: grid;
  place-items: center;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.tool-btn:active {
  transform: scale(0.94);
}

.tool-btn.active {
  box-shadow: 0 0 0 3px rgba(232, 160, 92, 0.45), var(--shadow);
}

.tool-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.tool-icon {
  display: grid;
  place-items: center;
  line-height: 1;
}

.mark-tool {
  font-size: 1.8rem;
  font-weight: 800;
  color: #c4a090;
}

.badge {
  position: absolute;
  top: -2px;
  right: -2px;
  min-width: 22px;
  height: 22px;
  padding: 0 6px;
  border-radius: 999px;
  background: var(--danger);
  color: #fff;
  font-size: 0.72rem;
  font-weight: 800;
  display: grid;
  place-items: center;
  box-shadow: 0 2px 6px rgba(232, 106, 92, 0.4);
}

/* —— Modal —— */
.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: grid;
  place-items: center;
  padding: 20px;
}

.modal[hidden] {
  display: none;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(40, 30, 25, 0.45);
  backdrop-filter: blur(2px);
}

.modal-sheet {
  position: relative;
  width: min(100%, 360px);
  background: var(--bg-soft);
  border-radius: 24px;
  padding: 24px 20px 20px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.18);
  text-align: center;
}

.modal-sheet h3 {
  margin: 0 0 16px;
  font-size: 1.25rem;
  font-weight: 800;
}

.switch-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  background: var(--surface);
  border-radius: 14px;
  padding: 14px 16px;
  margin-bottom: 10px;
  font-weight: 700;
  font-size: 0.95rem;
  text-align: left;
}

.switch-row input {
  width: 44px;
  height: 26px;
  accent-color: var(--accent);
}

.result-emoji {
  font-size: 3rem;
  line-height: 1;
  margin-bottom: 8px;
}

#result-desc {
  color: var(--text-soft);
  font-weight: 600;
  margin: 0 0 20px;
  line-height: 1.5;
}

.result-actions {
  display: flex;
  gap: 10px;
}

.result-actions .btn {
  flex: 1;
}

.hint-sheet {
  width: min(100%, 400px);
  padding-top: 18px;
}

.hint-msg {
  margin: 0 0 14px;
  font-weight: 700;
  color: var(--text);
  line-height: 1.5;
  font-size: 0.95rem;
  text-align: left;
  background: var(--surface);
  padding: 14px 16px;
  border-radius: 16px;
  box-shadow: var(--shadow);
}

.hint-board-wrap {
  margin-bottom: 16px;
}

.hint-board {
  width: 100%;
  pointer-events: none;
}

.hint-board .cell.hint-x::after {
  content: "×";
  font-size: clamp(0.9rem, 5vw, 1.5rem);
  font-weight: 800;
  color: rgba(255, 255, 255, 0.95);
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
}

/* —— Toast —— */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(28px + var(--safe-b));
  transform: translateX(-50%) translateY(12px);
  background: rgba(60, 42, 34, 0.92);
  color: #fff;
  padding: 12px 18px;
  border-radius: 999px;
  font-weight: 700;
  font-size: 0.9rem;
  z-index: 200;
  opacity: 0;
  transition: opacity 0.25s ease, transform 0.25s ease;
  pointer-events: none;
  max-width: calc(100% - 40px);
  text-align: center;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.toast[hidden] {
  display: block;
  visibility: hidden;
}

/* —— Responsive —— */
@media (max-width: 380px) {
  .rule-chip span:last-child {
    font-size: 0.62rem;
  }

  .tool-btn {
    width: 54px;
    height: 54px;
  }

  .toolbar {
    gap: 10px;
  }
}

@media (min-width: 600px) {
  #app {
    box-shadow: 0 0 0 1px rgba(107, 79, 66, 0.06), 0 20px 60px rgba(107, 79, 66, 0.08);
    min-height: 100dvh;
  }
}

@media (prefers-reduced-motion: reduce) {
  .menu-cat,
  .cat-face,
  .cell.invalid,
  .cell.hint-glow {
    animation: none !important;
  }

  * {
    transition-duration: 0.01ms !important;
  }
}
