:root {
  color-scheme: light;
  --paper: #f7f8f2;
  --surface: #ffffff;
  --surface-2: #eef5ed;
  --ink: #20312b;
  --muted: #6d7a74;
  --line: #cfdad3;
  --forest: #247052;
  --forest-dark: #194d3a;
  --leaf: #69a96f;
  --orange: #e75f3d;
  --orange-dark: #b94129;
  --yellow: #f3bd50;
  --sky: #bde0e8;
  --blue: #3f8da5;
  --danger: #c84242;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  font-synthesis: none;
}

* { box-sizing: border-box; }

html, body { width: 100%; min-height: 100%; margin: 0; background: var(--paper); }

body {
  color: var(--ink);
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  user-select: none;
  overscroll-behavior: none;
}

button, a { font: inherit; }
button { color: inherit; }
button:focus-visible, a:focus-visible { outline: 3px solid var(--yellow); outline-offset: 3px; }

.app-shell {
  min-height: 100svh;
  background-color: var(--paper);
  background-image: radial-gradient(#d7e1d9 1px, transparent 1px);
  background-size: 20px 20px;
}

.topbar {
  height: calc(64px + var(--safe-top));
  padding: var(--safe-top) max(18px, env(safe-area-inset-right)) 0 max(18px, env(safe-area-inset-left));
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--line);
  background: rgba(247, 248, 242, .94);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
}

.brand { display: flex; align-items: center; gap: 9px; color: var(--ink); text-decoration: none; font-weight: 900; font-size: 18px; }
.brand svg { width: 33px; height: 33px; flex: 0 0 auto; }
.header-actions { display: flex; gap: 8px; }

.player-button {
  min-width: 58px;
  max-width: 96px;
  height: 42px;
  padding: 0 11px;
  overflow: hidden;
  border: 1px solid var(--forest);
  border-radius: 7px;
  background: var(--surface);
  color: var(--forest-dark);
  font-size: 12px;
  font-weight: 900;
  text-overflow: ellipsis;
  white-space: nowrap;
  cursor: pointer;
}
.player-button:hover { background: var(--surface-2); }

.icon-button, .tool-button, .new-button {
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--surface);
  cursor: pointer;
}

.icon-button svg, .tool-button svg, .new-button svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.icon-button:hover, .tool-button:hover { border-color: #9eb2a5; background: var(--surface-2); }
.icon-button[aria-pressed="false"] { color: #9ba8a0; }
.tool-button:disabled { opacity: .36; cursor: default; }
.new-button { width: 48px; height: 48px; border-color: var(--forest); background: var(--forest); color: white; }
.new-button:hover { background: var(--forest-dark); }

main { padding: 18px max(12px, env(safe-area-inset-right)) calc(28px + var(--safe-bottom)) max(12px, env(safe-area-inset-left)); }
.game { width: min(100%, 720px); margin: 0 auto; }

.game-heading { min-height: 92px; display: flex; align-items: center; justify-content: space-between; gap: 16px; margin-bottom: 14px; }
.game-heading h1 { margin: 5px 0 0; font-size: clamp(22px, 4.8vw, 32px); line-height: 1.18; letter-spacing: 0; }
.game-heading img { width: min(36%, 190px); height: 88px; object-fit: contain; object-position: right center; }
.eyebrow { color: var(--forest); font-size: 12px; font-weight: 900; letter-spacing: 0; }

.difficulty {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 4px;
  width: min(100%, 430px);
  margin: 0 auto 14px;
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #e8eee8;
}

.difficulty button { height: 40px; border: 0; border-radius: 5px; background: transparent; color: var(--muted); font-weight: 800; cursor: pointer; }
.difficulty button[aria-pressed="true"] { background: var(--surface); color: var(--forest-dark); box-shadow: 0 1px 4px rgba(28, 65, 49, .14); }

.statusbar {
  min-height: 74px;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-bottom: 0;
  border-radius: 8px 8px 0 0;
  background: var(--surface);
}

.metric { display: flex; flex-direction: column; gap: 2px; }
.metric-right { text-align: right; }
.metric span { color: var(--muted); font-size: 12px; }
.metric strong { color: var(--orange-dark); font: 900 27px/1 ui-monospace, "SFMono-Regular", Consolas, monospace; }
.metric-right strong { color: var(--blue); font-size: 22px; white-space: nowrap; }
.round-actions { display: flex; align-items: center; gap: 6px; margin-inline: 10px; }

.limitbar { min-height: 38px; display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 0 14px; border: 1px solid var(--line); border-bottom: 0; background: #fff8e4; color: #725716; font-size: 12px; font-weight: 800; }
.limitbar > span { display: flex; align-items: center; gap: 7px; }
.limitbar svg { width: 17px; height: 17px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.limitbar strong { font: 900 16px/1 ui-monospace, "SFMono-Regular", Consolas, monospace; }
.limitbar.resting { background: #ffe8e4; color: var(--danger); }
.limit-actions { display: flex; align-items: center; gap: 10px; }
.limit-pause-button { width: 30px; height: 28px; display: grid; place-items: center; padding: 0; border: 1px solid #d6a942; border-radius: 5px; background: white; color: #725716; cursor: pointer; }
.limit-pause-button svg { grid-area: 1 / 1; width: 18px; height: 18px; }
.limit-pause-button .play-arrow { display: none; fill: currentColor; }
.limit-pause-button.is-paused .pause-bars { display: none; }
.limit-pause-button.is-paused .play-arrow { display: block; }
.limit-pause-button:disabled { opacity: .4; cursor: default; }

.board-panel { position: relative; border: 1px solid var(--line); border-radius: 0 0 8px 8px; background: #e9f1eb; overflow: hidden; }
.board-scroll { width: 100%; padding: clamp(8px, 2.5vw, 18px); overflow: auto; }
.puzzle-grid {
  --size: 5;
  display: grid;
  grid-template-columns: 32px repeat(var(--size), minmax(0, 1fr));
  gap: clamp(2px, .7vw, 5px);
  width: min(100%, 610px);
  margin: 0 auto;
  touch-action: manipulation;
}

.corner, .clue, .cell { min-width: 0; aspect-ratio: 1; }
.corner { display: grid; place-items: center; color: #91a19a; }
.corner svg { width: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }
.clue { display: grid; place-items: center; color: var(--muted); font-weight: 900; font-size: clamp(13px, 3vw, 20px); }
.clue.complete { color: var(--forest); }
.clue.over { color: var(--danger); animation: nudge .25s ease; }

.cell {
  position: relative;
  display: grid;
  place-items: center;
  padding: 0;
  border: 1px solid #b9c9bf;
  border-radius: min(6px, 14%);
  background: #fbfcf8;
  cursor: pointer;
  overflow: hidden;
  transform: translateZ(0);
  transition: transform 100ms ease, background 130ms ease, border-color 130ms ease;
}

.cell:hover { border-color: #7f9b8a; background: white; }
.cell:active { transform: scale(.92); }
.cell.tree { cursor: default; border-color: #9fc2a0; background: #dcebd8; }
.cell.grass { background: #f1f4ed; }
.cell.grass::before, .cell.grass::after { content: ""; position: absolute; bottom: 20%; width: 2px; height: 32%; border-radius: 2px; background: #8cad7f; transform-origin: bottom; }
.cell.grass::before { left: 43%; transform: rotate(-35deg); box-shadow: 8px 1px #8cad7f; }
.cell.grass::after { left: 55%; transform: rotate(25deg); }
.cell svg { width: 76%; height: 76%; overflow: visible; }
.tree-icon .trunk { fill: #8d573c; }
.tree-icon .crown { fill: var(--forest); stroke: var(--forest-dark); stroke-width: 1.2; }
.tree-icon .shine { fill: #8bc27f; }
.cell.just-pitched .tent-icon { animation: pitch .22s cubic-bezier(.2,.9,.3,1.3) both; }
.tent-icon .canvas { fill: var(--orange); stroke: var(--orange-dark); stroke-width: 1.4; stroke-linejoin: round; }
.tent-icon .door { fill: var(--yellow); stroke: var(--orange-dark); stroke-width: 1.2; stroke-linejoin: round; }
.tent-icon .pole { stroke: #fff9ef; stroke-width: 1.5; }
.tent-icon .ground { stroke: var(--forest-dark); stroke-width: 1.8; stroke-linecap: round; }
.cell.conflict { border-color: var(--danger); background: #ffe8e4; animation: nudge .25s ease; }
.cell.reveal-solution:not(.tent):not(.tree)::after { content: ""; width: 10px; aspect-ratio: 1; border-radius: 50%; background: #b8c5bd; }

.result, .pause-overlay, .rest-overlay {
  position: absolute;
  inset: 0;
  z-index: 5;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 22px;
  text-align: center;
  background: rgba(247, 248, 242, .95);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}

.result[hidden], .pause-overlay[hidden], .rest-overlay[hidden] { display: none; }
.result-badge { width: 68px; height: 68px; display: grid; place-items: center; border-radius: 50%; background: var(--yellow); }
.result-badge svg { width: 46px; fill: var(--orange); stroke: var(--forest-dark); stroke-width: 2.5; stroke-linejoin: round; }
.result .eyebrow { margin-top: 12px; }
.result h2 { margin: 5px 0 8px; font-size: clamp(23px, 5vw, 30px); letter-spacing: 0; }
.result p { margin: 0 0 18px; color: var(--muted); }
.result-actions { display: flex; gap: 8px; }
.primary-button, .secondary-button {
  min-height: 46px;
  padding: 0 20px;
  border: 1px solid var(--forest);
  border-radius: 7px;
  font-weight: 900;
  cursor: pointer;
}
.primary-button { background: var(--forest); color: white; }
.primary-button:hover { background: var(--forest-dark); }
.secondary-button { background: white; color: var(--forest-dark); }

.control-dock { margin-top: 14px; }
.mode-switch { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; padding: 4px; border: 1px solid var(--line); border-radius: 8px; background: #e8eee8; }
.mode-switch button {
  height: 52px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: 0;
  border-radius: 5px;
  background: transparent;
  color: var(--muted);
  font-weight: 900;
  cursor: pointer;
}
.mode-switch button[aria-pressed="true"] { background: white; color: var(--forest-dark); box-shadow: 0 1px 4px rgba(28, 65, 49, .14); }
.mode-switch svg { width: 28px; height: 28px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linejoin: round; }
.mode-switch button[data-mode="tent"] svg path:first-child { fill: currentColor; stroke: currentColor; }
.mode-switch button[data-mode="tent"] svg path:last-child { stroke: white; }

.rest-overlay { z-index: 7; text-align: center; background: rgba(247, 248, 242, .97); }
.pause-overlay { z-index: 6; text-align: center; }
.pause-symbol { width: 62px; height: 62px; display: grid; place-items: center; border-radius: 50%; background: #e6f2e3; border: 1px solid #9fc2a0; color: var(--forest); font-size: 25px; font-weight: 900; }
.pause-overlay h2 { margin: 16px 0 7px; font-size: 27px; letter-spacing: 0; }
.pause-overlay p { margin: 0 0 17px; color: var(--muted); }
.rest-symbol { width: 62px; height: 62px; display: grid; place-items: center; border-radius: 50%; background: #fff0c7; border: 1px solid #d6a942; color: #725716; font-size: 24px; font-weight: 900; }
.rest-overlay h2 { margin: 16px 0 7px; font-size: 27px; letter-spacing: 0; }
.rest-overlay p { max-width: 320px; margin: 0 0 15px; color: var(--muted); line-height: 1.6; }
.rest-overlay > strong { color: var(--danger); font: 900 20px/1.2 ui-monospace, "SFMono-Regular", Consolas, monospace; }
.game.is-resting .difficulty, .game.is-resting .statusbar, .game.is-resting .control-dock { pointer-events: none; }
.game.is-paused .difficulty, .game.is-paused .statusbar, .game.is-paused .control-dock { pointer-events: none; }

.progress-line { margin-top: 14px; display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 12px; color: var(--muted); font-size: 12px; }
.progress-line > div:first-child { display: flex; align-items: baseline; gap: 5px; }
.progress-line strong { color: var(--forest); font-size: 18px; }
.progress-track { height: 7px; border-radius: 4px; background: #dbe4dc; overflow: hidden; }
.progress-track i { display: block; width: 0; height: 100%; border-radius: inherit; background: var(--orange); transition: width .35s ease; }

.leaderboard { width: min(100%, 520px); margin: 18px auto 0; padding-top: 16px; border-top: 1px solid var(--line); }
.leaderboard-head { min-height: 28px; display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.leaderboard-head h2 { margin: 0; font-size: 15px; letter-spacing: 0; }
.leaderboard-head span { color: var(--muted); font-size: 11px; white-space: nowrap; }
.leaderboard-head span[data-state="ready"] { color: var(--forest); }
.leaderboard-head span[data-state="error"] { color: var(--danger); }
.leaderboard-list { margin: 8px 0 0; padding: 0; list-style: none; }
.leaderboard-list li {
  min-height: 36px;
  display: grid;
  grid-template-columns: 26px 1fr auto;
  align-items: center;
  gap: 8px;
  border-bottom: 1px solid #e3ebe5;
  color: var(--muted);
  font-size: 12px;
}
.leaderboard-list li:last-child { border-bottom: 0; }
.leaderboard-list li > span:nth-child(2) { min-width: 0; overflow: hidden; color: var(--ink); text-overflow: ellipsis; white-space: nowrap; }
.leaderboard-list .rank { width: 22px; height: 22px; display: grid; place-items: center; border: 1px solid #a8bdb0; border-radius: 50%; color: var(--forest-dark); font-weight: 900; }
.leaderboard-list strong { color: var(--orange-dark); font: 900 14px/1 ui-monospace, "SFMono-Regular", Consolas, monospace; }

.quick-rules { margin-top: 18px; padding: 16px 0; border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); }
.quick-rules-head { display: flex; align-items: center; gap: 8px; margin-bottom: 13px; }
.quick-rules-head svg { width: 23px; height: 23px; fill: none; stroke: var(--forest); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.quick-rules-head h2 { margin: 0; font-size: 17px; letter-spacing: 0; }
.logic-badge { margin-left: auto; padding: 4px 8px; border: 1px solid #9fc2a0; border-radius: 5px; background: #e6f2e3; color: var(--forest-dark); font-size: 11px; font-weight: 900; }
.quick-rules-list { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; }
.quick-rules-list > div { display: grid; grid-template-columns: 27px 1fr; align-items: start; gap: 8px; min-width: 0; }
.quick-rules-list > div > span { width: 27px; height: 27px; display: grid; place-items: center; border-radius: 50%; background: var(--forest); color: white; font-size: 13px; font-weight: 900; }
.quick-rules-list p { margin: 0; color: var(--muted); font-size: 12px; line-height: 1.55; }
.quick-rules-list strong { display: block; margin-bottom: 2px; color: var(--ink); font-size: 13px; }

dialog {
  width: min(calc(100% - 28px), 520px);
  max-height: min(90svh, 700px);
  padding: 22px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--paper);
  color: var(--ink);
  box-shadow: 0 22px 70px rgba(30, 55, 44, .26);
  overflow: auto;
}
dialog::backdrop { background: rgba(25, 50, 40, .56); backdrop-filter: blur(3px); }
.dialog-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; }
.dialog-head h2 { margin: 4px 0 0; font-size: 26px; }
.close-button { flex: 0 0 auto; }
.rules { display: grid; gap: 14px; margin: 22px 0; }
.rule { display: grid; grid-template-columns: 34px 1fr; gap: 11px; align-items: start; }
.rule-number { width: 31px; height: 31px; display: grid; place-items: center; border-radius: 50%; background: var(--forest); color: white; font-weight: 900; }
.rule strong { font-size: 15px; }
.rule p { margin: 3px 0 0; color: var(--muted); font-size: 13px; line-height: 1.55; }
.mini-example { display: grid; grid-template-columns: repeat(4, 34px); gap: 3px; justify-content: center; margin: 6px 0 22px; }
.mini-example > * { display: grid; place-items: center; aspect-ratio: 1; border: 1px solid #c9d5cd; border-radius: 4px; background: white; font-style: normal; }
.mini-example .clue { border-color: transparent; background: transparent; }
.tree-demo { color: var(--forest); font-size: 24px; }
.tent-demo { position: relative; }
.tent-demo::before { content: ""; width: 22px; height: 20px; background: var(--orange); clip-path: polygon(50% 0, 100% 100%, 0 100%); }
.dialog-primary { width: 100%; }
.player-form { margin-top: 20px; }
.player-form label { display: block; margin-bottom: 7px; color: var(--muted); font-size: 12px; font-weight: 800; }
.player-form select, .player-form input {
  width: 100%;
  height: 46px;
  padding: 0 12px;
  border: 1px solid #a9bbb0;
  border-radius: 6px;
  background: white;
  color: var(--ink);
  font: inherit;
  outline: none;
}
.player-form select:focus, .player-form input:focus { border-color: var(--forest); box-shadow: 0 0 0 2px rgba(36, 112, 82, .14); }
.form-separator { height: 34px; display: flex; align-items: center; gap: 10px; color: #8a9891; font-size: 11px; }
.form-separator::before, .form-separator::after { content: ""; height: 1px; flex: 1; background: var(--line); }
.form-error { margin: 10px 0 0; color: var(--danger); font-size: 12px; line-height: 1.5; }
.player-submit { width: 100%; margin-top: 18px; }

.toast {
  position: fixed;
  z-index: 20;
  left: 50%;
  bottom: calc(20px + var(--safe-bottom));
  max-width: calc(100% - 32px);
  padding: 11px 16px;
  border-radius: 6px;
  background: var(--ink);
  color: white;
  font-size: 14px;
  font-weight: 800;
  text-align: center;
  opacity: 0;
  transform: translate(-50%, 16px);
  pointer-events: none;
  transition: opacity .2s ease, transform .2s ease;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@keyframes pitch { from { opacity: 0; transform: translateY(8px) scale(.72); } to { opacity: 1; transform: none; } }
@keyframes nudge { 25% { transform: translateX(-2px); } 70% { transform: translateX(2px); } }

@media (max-width: 520px) {
  main { padding-top: 10px; }
  .game-heading { min-height: 70px; margin-bottom: 8px; }
  .game-heading img { width: 33%; height: 68px; }
  .game-heading h1 { max-width: 260px; }
  .statusbar { min-height: 68px; padding-inline: 8px; }
  .round-actions { gap: 4px; margin-inline: 5px; }
  .tool-button { width: 38px; height: 40px; }
  .new-button { width: 42px; height: 44px; }
  .puzzle-grid { grid-template-columns: 26px repeat(var(--size), minmax(0, 1fr)); }
  .progress-line { gap: 8px; }
  .quick-rules { padding-block: 14px; }
  .quick-rules-list { grid-template-columns: 1fr; gap: 12px; }
}

@media (max-width: 370px) {
  .brand span { font-size: 16px; }
  .header-actions { gap: 5px; }
  .player-button { min-width: 50px; max-width: 64px; height: 38px; padding-inline: 7px; }
  .icon-button { width: 38px; height: 38px; }
  .game-heading h1 { font-size: 21px; }
  .metric strong { font-size: 23px; }
  .metric-right strong { font-size: 18px; }
  .tool-button { width: 34px; }
  .new-button { width: 38px; }
  .round-actions { margin-inline: 3px; }
  .mode-switch button { gap: 4px; font-size: 14px; }
  .mode-switch svg { width: 24px; }
  .progress-line { grid-template-columns: auto 1fr; }
  .progress-line > span { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
}
