* { box-sizing: border-box; }

:root {
  --bg: #f5f5f5;
  --text: #222;
  --card: #fff;
  --muted: #666;
  --line: #d5d5d5;
  --strong: #222;
  --token-bg: #166857;
  --token-text: #fff;
  --accent-soft: #e7f3ef;
  --accent-mid: #8fb7ac;
  --accent-strong: #166857;
  --available-bg: #eef7f3;
  --available-text: #295e53;
  --glow-1: rgba(70, 165, 135, .48);
  --glow-2: rgba(60, 145, 118, .28);
  --glow-3: rgba(48, 116, 92, .12);
  --flash: #dff5e9;
  --best-success: #1e7a1e;
  --close: #c47a00;
}

body.theme-max {
  --token-bg: #d97322;
  --token-text: #fff;
  --accent-soft: #fdf0e6;
  --accent-mid: #e2a06b;
  --accent-strong: #d97322;
  --available-bg: #fff2e8;
  --available-text: #9a4f12;
  --glow-1: rgba(235, 144, 66, .46);
  --glow-2: rgba(217, 115, 34, .26);
  --glow-3: rgba(176, 84, 16, .12);
  --flash: #fff0e1;
}

body.theme-zero {
  --token-bg: #166857;
  --token-text: #fff;
  --accent-soft: #e7f3ef;
  --accent-mid: #8fb7ac;
  --accent-strong: #166857;
  --available-bg: #eef7f3;
  --available-text: #295e53;
  --glow-1: rgba(70, 165, 135, .48);
  --glow-2: rgba(60, 145, 118, .28);
  --glow-3: rgba(48, 116, 92, .12);
  --flash: #dff5e9;
}

body {
  margin: 0;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

[hidden] { display: none !important; }

.wrap { max-width: 620px; margin: 0 auto; padding: 18px 16px 40px; }

.toprow {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 8px;
}

h1 { margin: 0; font-size: 22px; line-height: 1; }

.rule-switch { display: flex; gap: 6px; }

.rule-btn {
  padding: 7px 12px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: #fff;
  color: #111;
  font: inherit;
  font-weight: 800;
  cursor: pointer;
  transition: transform .15s ease, background .15s ease, border-color .15s ease, box-shadow .15s ease, color .15s ease;
}
.rule-btn.active {
  background: var(--strong);
  color: #fff;
  border-color: var(--strong);
  box-shadow: inset 0 0 0 1px rgba(255,255,255,.06);
}
body.theme-max .rule-btn[data-rule="max"].active {
  background: var(--accent-strong);
  border-color: var(--accent-strong);
}
body.theme-zero .rule-btn[data-rule="zero"].active {
  background: var(--accent-strong);
  border-color: var(--accent-strong);
}
.rule-btn:active,
.mode-btn:active,
button:active { transform: scale(.985); }

.rule-guide {
  margin: 0 0 14px;
  font-size: 13px;
  line-height: 1.35;
  color: var(--muted);
}

.modebar {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-bottom: 14px;
}
.mode-btn {
  padding: 10px 8px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  color: #111;
  font: inherit;
  font-weight: 800;
  cursor: pointer;
  transition: transform .15s ease, background .15s ease, border-color .15s ease, box-shadow .15s ease, color .15s ease;
}
.mode-btn.active {
  background: var(--strong);
  color: #fff;
  border-color: var(--strong);
}
body.theme-max .mode-btn.active {
  background: var(--accent-strong);
  border-color: var(--accent-strong);
}
body.theme-zero .mode-btn.active {
  background: var(--accent-strong);
  border-color: var(--accent-strong);
}

.status {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 12px;
  margin-bottom: 16px;
}
.status-col-left { justify-self: start; min-width: 0; }
.status-col-center { justify-self: center; }
.status-col-right { justify-self: end; min-width: 0; }

.status-pill,
.moves-box {
  display: inline-flex;
  align-items: baseline;
  gap: 6px;
  min-height: 44px;
  padding: 8px 12px;
  border-radius: 999px;
  white-space: nowrap;
}
.status-pill .label,
.moves-box .label {
  font-size: 16px;
  font-weight: 700;
  opacity: .84;
}
.status-pill .value,
.moves-box .value {
  font-size: 24px;
  font-weight: 900;
  line-height: 1;
}
.status-pill {
  background: #fff;
  border: 1px solid rgba(0,0,0,.05);
}
.moves-box {
  color: #333;
  transition: color .15s ease, transform .15s ease;
  background: color-mix(in srgb, var(--accent-soft) 50%, #fff);
  border: 2px solid color-mix(in srgb, var(--accent-mid) 30%, transparent);
}
.moves-box.warning { color: #b30000; }
.moves-box.danger { color: #9b0000; transform: scale(1.06); animation: pulse .7s infinite alternate; }
@keyframes pulse { from { transform: scale(1.02); } to { transform: scale(1.07); } }

.current-pill {
  border: 1px solid rgba(0,0,0,.05);
}
.best-pill {
  border: 1px solid rgba(0,0,0,.05);
}
.best-pill.pop {
  animation: bestPop .5s ease;
}
@keyframes bestPop {
  0% { transform: scale(1); }
  35% { transform: scale(1.08); }
  100% { transform: scale(1); }
}

.board-frame { border-radius: 14px; background: #fff; padding: 8px; overflow: hidden; }
.board-wrap { position: relative; overflow: hidden; border-radius: 10px; min-height: 320px; }
.board { display: grid; gap: 8px; position: relative; z-index: 2; }

.cell {
  position: relative;
  aspect-ratio: 1 / 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #fff;
  border-radius: 10px;
  user-select: none;
  transition: background .24s ease, color .24s ease, box-shadow .24s ease, opacity .24s ease, transform .18s ease, filter .28s ease;
}
.cell:active { transform: scale(.99); }
.cell.picked { background: #e6e6e6; box-shadow: 0 0 0 3px rgba(0,0,0,.08); }
.cell.absorbing { animation: absorbShrink .22s ease forwards; transform-origin: center center; }
@keyframes absorbShrink {
  0% { transform: scale(1); opacity: 1; }
  55% { transform: scale(.68); opacity: .88; }
  100% { transform: scale(.18); opacity: 0; }
}

.cell.fade-from-player { animation: fadeFromPlayer .28s ease forwards; }
@keyframes fadeFromPlayer {
  0% { background: var(--token-bg); opacity: 1; transform: scale(1); }
  30% { background: linear-gradient(180deg, color-mix(in srgb, var(--token-bg) 72%, #ffffff 28%) 0%, color-mix(in srgb, var(--token-bg) 54%, #ffffff 46%) 100%); opacity: .95; transform: scale(.985); }
  100% { opacity: 1; transform: scale(1); }
}

.cell.dissolve-in { animation: cellDissolve .42s cubic-bezier(.22,.8,.2,1) forwards; }
@keyframes cellDissolve {
  0% { opacity: .9; transform: scale(.98); }
  100% { opacity: 1; transform: scale(1); }
}

.fog { background: linear-gradient(180deg, #d6d6d6 0%, #cfcfcf 100%); color: transparent; }
.empty { background: #f0f0f0; color: transparent; }
.revealed-near { background: #fafafa; color: #666; }
.revealed-far { background: linear-gradient(180deg, #f4f4f4 0%, #ececec 100%); color: #9a9a9a; }
.available {
  background: var(--available-bg);
  color: var(--available-text);
  box-shadow: inset 0 0 0 2px var(--accent-mid);
}
.available.empty { color: transparent; }
.negative { color: #aa2222; }
.available.negative { color: #8a1111; }
.revealed-far.negative { color: #c08a8a; }
.player-cell-placeholder { background: var(--token-bg); color: transparent; }

.cell-text {
  display: inline-block;
  font-weight: 700;
  font-size: 18px;
  line-height: 1;
  transform: scale(1);
  transform-origin: center center;
  opacity: 1;
  transition: font-size .42s ease, transform .42s ease, color .22s ease, opacity .22s ease, filter .30s ease;
}
.cell-text.t-near { font-size: 18px; transform: scale(1); }
.cell-text.t-far { font-size: 16px; transform: scale(.97); }
.cell-text.t-available { font-size: 21px; transform: scale(1.04); font-weight: 800; }
.cell-text.dissolve-in { animation: dissolveIn .22s ease-out forwards; }
@keyframes dissolveIn {
  0% { opacity: .74; transform: scale(.97); }
  100% { opacity: 1; transform: scale(1); }
}

.m-dist-1 { opacity: 1; filter: blur(0); }
.m-dist-2 { opacity: .84; filter: blur(.15px); }
.m-dist-3 { opacity: .62; filter: blur(.35px); }
.m-dist-4 { opacity: .36; filter: blur(.65px); }
.m-dist-5 { opacity: .18; filter: blur(1px); }
.m-dist-6 { opacity: .08; filter: blur(1.35px); }

.m-fade-0 {}
.m-fade-1 { opacity: calc(var(--memory-opacity, 1) * .72); }
.m-fade-2 { opacity: calc(var(--memory-opacity, 1) * .52); }
.m-fade-3 { opacity: calc(var(--memory-opacity, 1) * .34); }
.m-fade-4 { opacity: calc(var(--memory-opacity, 1) * .22); }
.m-fade-5 { opacity: calc(var(--memory-opacity, 1) * .16); }
.m-fade-6 { opacity: calc(var(--memory-opacity, 1) * .12); }
.m-fade-7 { opacity: calc(var(--memory-opacity, 1) * .10); }

.player-token {
  position: absolute;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--token-bg);
  color: var(--token-text);
  font-weight: 900;
  border-radius: 10px;
  z-index: 5;
  pointer-events: none;
  transition: left .34s cubic-bezier(.2,.8,.2,1), top .34s cubic-bezier(.2,.8,.2,1), width .34s cubic-bezier(.2,.8,.2,1), height .34s cubic-bezier(.2,.8,.2,1), transform .18s ease, box-shadow .2s ease;
  box-shadow: 0 6px 16px rgba(0,0,0,.12);
}
.player-token.idle { animation: breathe 1.8s ease-in-out infinite alternate; }
@keyframes breathe { 0% { transform: scale(1); } 100% { transform: scale(1.018); } }
.player-token.absorb-pop { animation: playerAbsorbPop .22s ease; }
.player-token.landing { animation: landingBounce .24s ease; }
.player-token.zero-perfect { animation: perfectPulse .5s ease; }
@keyframes playerAbsorbPop { 0% { transform: scale(1); } 45% { transform: scale(1.08); } 100% { transform: scale(1); } }
@keyframes landingBounce { 0% { transform: scale(1); } 35% { transform: scale(.96); } 70% { transform: scale(1.04); } 100% { transform: scale(1); } }
@keyframes perfectPulse {
  0% { transform: scale(1); box-shadow: 0 6px 16px rgba(0,0,0,.12); }
  45% { transform: scale(1.12); box-shadow: 0 0 0 10px color-mix(in srgb, var(--token-bg) 20%, transparent); }
  100% { transform: scale(1); box-shadow: 0 6px 16px rgba(0,0,0,.12); }
}

.absorb-token {
  position: absolute;
  z-index: 6;
  pointer-events: none;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 10px;
  font-weight: 800;
  transition: left .22s ease, top .22s ease, width .22s ease, height .22s ease, transform .22s ease, opacity .22s ease;
}
.absorb-token.negative { background: rgba(170,34,34,.10); color: #aa2222; }
.absorb-token.positive { background: rgba(11,93,58,.10); color: #0b5d3a; }

.vision-glow {
  position: absolute;
  z-index: 1;
  pointer-events: none;
  border-radius: 999px;
  background: radial-gradient(circle, var(--glow-1) 0%, var(--glow-2) 35%, var(--glow-3) 60%, rgba(0,0,0,0) 100%);
  mix-blend-mode: multiply;
  filter: blur(4px);
  transition: left .55s cubic-bezier(.22,.8,.2,1), top .55s cubic-bezier(.22,.8,.2,1), width .55s cubic-bezier(.22,.8,.2,1), height .55s cubic-bezier(.22,.8,.2,1);
}

.actions { display: flex; justify-content: center; margin-top: 16px; }

button {
  padding: 8px 14px;
  border: 0;
  border-radius: 8px;
  background: #fff;
  cursor: pointer;
  font: inherit;
}

#playAgain { border: 1px solid #bbb; }

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.4);
  display: grid;
  place-items: center;
  padding: 20px;
  z-index: 100;
}
.resultCard {
  width: min(100%, 360px);
  background: #fff;
  border-radius: 16px;
  padding: 24px 20px;
  text-align: center;
  box-shadow: 0 12px 40px rgba(0,0,0,.18);
  border: 2px solid color-mix(in srgb, var(--accent-strong) 30%, #ffffff 70%);
}
.resultLabel { font-size: 12px; letter-spacing: .12em; color: #666; margin-bottom: 10px; }
.resultScore {
  font-size: 34px;
  font-weight: 900;
  margin-bottom: 8px;
  line-height: 1.05;
}
.subScore {
  color: #666;
  margin-bottom: 12px;
  min-height: 24px;
}
.bestMessage {
  min-height: 30px;
  margin-bottom: 16px;
  font-size: 18px;
  font-weight: 800;
}
.bestMessage.success { color: var(--best-success); }
.bestMessage.close { color: var(--close); }
.bestMessage.fail { color: #8e8e8e; }

body.flash-success { animation: flashSuccess .45s; }
@keyframes flashSuccess { 0% { background: var(--flash); } 100% { background: var(--bg); } }

@media (max-width: 560px) {
  .status {
    grid-template-columns: 1fr 1fr 1fr;
    gap: 6px;
  }

  .status-col-left,
  .status-col-center,
  .status-col-right {
    justify-self: stretch;
  }

  .status-pill,
  .moves-box {
    width: 100%;
    min-height: 38px;
    padding: 6px 6px;
    justify-content: center;
    gap: 4px;
  }

  .status-pill .label,
  .moves-box .label {
    font-size: 11px;
  }

  .status-pill .value,
  .moves-box .value {
    font-size: 20px;
  }
  .status-col-left,
  .status-col-center,
  .status-col-right {
    justify-self: stretch;
  }
  .status-pill,
  .moves-box {
    width: 100%;
    justify-content: center;
  }
  .toprow {
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
  }
}
