:root {
  --ink: #5a4030;
  --ink-soft: #7a624e;
  --paper: #fffaf3;
  --cream: #f6ecdf;
  --line: #e4d2bc;
  --peach: #f0b56a;
  --peach-deep: #d9943f;
  --sage: #6f8f72;
  --gold: #e2a122;
  --gold-dim: #d3c3ae;
  --shadow: 0 10px 28px rgba(92, 62, 32, 0.12);
  color: var(--ink);
  font-family: "Avenir Next", "Segoe UI", "PingFang SC", "Hiragino Sans", sans-serif;
  background: #f3e6d4;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
}

body {
  background-color: #f3e6d4;
  background-image:
    linear-gradient(rgba(243, 230, 212, 0.9), rgba(243, 230, 212, 0.94)),
    url("../assets/sprites/floor.png?v=3.5");
  background-size: auto, 640px;
}

button, input { font: inherit; color: inherit; }

button { cursor: pointer; }

.hidden { display: none !important; }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

.screen {
  width: min(960px, 100%);
  margin: 0 auto;
  padding: 18px 16px 28px;
}

.hero { text-align: center; }

.cover {
  width: min(100%, 820px);
  height: min(38vw, 280px);
  object-fit: cover;
  object-position: center 40%;
  border-radius: 22px;
  display: block;
  margin: 0 auto 16px;
  box-shadow: var(--shadow);
}

h1 {
  font-size: clamp(32px, 5vw, 48px);
  line-height: 1;
  margin: 0;
  letter-spacing: -0.03em;
}

.tagline {
  margin: 8px 0 0;
  color: var(--ink-soft);
  font-size: 16px;
}

.version-tag {
  margin: 0;
  color: var(--ink-soft);
  font-size: 13px;
  font-weight: 800;
  letter-spacing: 0.08em;
}

.hero .version-tag { margin-top: 10px; }

.savings-readout {
  margin: 8px 0 0;
  color: var(--ink);
  font-size: 18px;
  font-weight: 800;
}

#levels-savings { margin: -4px 0 12px; }

.head-spacer {
  justify-self: end;
  display: flex;
  justify-content: flex-end;
}

.head-spacer .version-tag {
  background: var(--paper);
  border-radius: 999px;
  padding: 6px 10px;
  box-shadow: 0 3px 0 #eadccb;
}

h2 {
  margin: 22px 0 10px;
  font-size: 15px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--ink-soft);
}

.characters {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
}

.char-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 10px 8px 14px;
  background: var(--paper);
  border: 3px solid transparent;
  border-radius: 20px;
  box-shadow: var(--shadow);
}

.char-card.selected {
  border-color: var(--sage);
  box-shadow: 0 0 0 4px rgba(111, 143, 114, 0.22), var(--shadow);
}

.char-art, .char-art.fallback {
  width: 100%;
  height: 190px;
  object-fit: contain;
}

.char-art.fallback {
  display: grid;
  place-items: center;
  border-radius: 16px;
  color: white;
  font-size: 42px;
  font-weight: 800;
}

.char-name { font-weight: 800; font-size: 18px; }

.char-blurb {
  color: var(--ink-soft);
  font-size: 13px;
  line-height: 1.3;
  min-height: 2.6em;
}

.actions {
  margin-top: 18px;
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 10px;
}

.title-extra,
.legal-links {
  margin: 14px 0 0;
  text-align: center;
}

.legal-links {
  margin-top: 22px;
  font-size: 12px;
  color: var(--ink-soft);
}

.legal-links a,
.tip-link {
  color: var(--ink-soft);
  font-size: 13px;
  font-weight: 700;
  text-decoration: underline;
  text-underline-offset: 3px;
}

.legal-links a { font-size: 12px; font-weight: 600; }

.story-card .tip-link { margin-top: 12px; }

.legal {
  width: min(680px, 100%);
  margin: 0 auto;
  padding: 28px 18px 48px;
}

.legal h1 {
  margin-top: 12px;
  font-size: clamp(28px, 4vw, 40px);
}

.legal p,
.legal li {
  line-height: 1.5;
  font-size: 16px;
}

.legal a { color: var(--ink); }

.legal-back { margin: 0; font-size: 14px; }

.placeholder {
  font-family: ui-monospace, "SFMono-Regular", Consolas, monospace;
  background: var(--paper);
  border-radius: 6px;
  padding: 1px 6px;
}

.btn {
  appearance: none;
  border: 2px solid var(--line);
  background: var(--paper);
  border-radius: 16px;
  padding: 12px 18px;
  font-weight: 800;
  box-shadow: 0 3px 0 #e4d0b6;
}

.btn:active { transform: translateY(2px); box-shadow: none; }

.btn:disabled {
  opacity: 0.45;
  cursor: default;
  transform: none;
}

.btn.primary {
  background: var(--peach);
  border-color: #e3aa5d;
  box-shadow: 0 3px 0 var(--peach-deep);
  min-width: 180px;
}

.btn.wide { width: 100%; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.play-head {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 12px;
}

.play-head .btn { justify-self: start; }

.clerk-chip {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--paper);
  border-radius: 999px;
  padding: 4px 12px 4px 4px;
  box-shadow: var(--shadow);
}

.clerk-chip img {
  width: 42px;
  height: 42px;
  object-fit: contain;
}

.progress {
  margin: 14px 0;
  color: var(--ink-soft);
  font-weight: 700;
}

.level-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 10px;
}

.level-card {
  text-align: left;
  background: var(--paper);
  border: 2px solid var(--line);
  border-radius: 16px;
  padding: 10px 12px 12px;
  box-shadow: 0 3px 0 #eadccb;
  min-height: 96px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.level-card.locked { opacity: 0.55; }

.lv-num {
  font-size: 12px;
  letter-spacing: 0.08em;
  color: var(--ink-soft);
  font-weight: 800;
}

.lv-name { font-weight: 800; }

.lv-meta {
  margin-top: auto;
  display: flex;
  justify-content: space-between;
  gap: 6px;
  font-size: 13px;
}

.stars { letter-spacing: 1px; color: var(--gold-dim); font-weight: 800; }
.stars.s1, .stars.s2 { color: var(--gold); }

.lv-par { color: var(--ink-soft); }

.help {
  color: var(--ink-soft);
  font-size: 14px;
  line-height: 1.45;
}

.play {
  height: 100dvh;
  max-height: 100dvh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  width: min(980px, 100%);
  padding-top: 12px;
  padding-bottom: 10px;
}

.play-titles { text-align: center; min-width: 0; }

.play-level, .play-kinds {
  margin: 0;
  color: var(--ink-soft);
  font-size: 13px;
  font-weight: 700;
}

.play-name {
  margin: 0;
  font-size: clamp(20px, 3vw, 28px);
  letter-spacing: -0.02em;
  text-transform: none;
  color: var(--ink);
}

.hud {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px 16px;
  margin: 8px 0 4px;
  font-weight: 800;
}

.hint-btn { padding: 8px 14px; }

.hud .over { color: #b8613d; }

.hud .savings-readout {
  margin: 0;
  font-size: 16px;
  background: var(--paper);
  border-radius: 999px;
  padding: 6px 12px;
  box-shadow: 0 3px 0 #eadccb;
}

.pace { color: var(--sage); }
.pace.over { color: #b8613d; }

.legend {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 14px;
  margin: 4px 0 8px;
  font-weight: 800;
}

.legend-item {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: rgba(255, 250, 243, 0.8);
  border-radius: 999px;
  padding: 2px 10px 2px 4px;
}

.legend-art {
  width: 36px;
  height: 36px;
  object-fit: contain;
}

.legend-art.fallback {
  display: grid;
  place-items: center;
  border-radius: 8px;
  color: white;
  font-size: 14px;
}

.legend-name { font-size: 13px; font-weight: 800; }

.board-scroll {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  align-items: safe center;
  justify-content: safe center;
  overflow: auto;
  padding: 16px 8px 8px;
}

.board {
  --cell: min(64px, calc((min(100vw, 920px) - 28px) / var(--cols)), calc((100dvh - 340px) / var(--rows)));
  width: calc(var(--cell) * var(--cols));
  height: calc(var(--cell) * var(--rows));
  position: relative;
  isolation: isolate;
  border-radius: 18px;
  background-color: #e7c48d;
  background-image: url("../assets/sprites/floor.png?v=3.5");
  background-size: calc(var(--cell) * 4.2);
  box-shadow:
    0 18px 40px rgba(90, 55, 25, 0.18),
    inset 0 0 0 3px rgba(255, 248, 236, 0.45);
}

.tiles {
  display: grid;
  grid-template-columns: repeat(var(--cols), var(--cell));
  grid-template-rows: repeat(var(--rows), var(--cell));
  width: 100%;
  height: 100%;
}

.cell { position: relative; }

.cell.wall {
  background-image: url("../assets/sprites/wall.png?v=3.5");
  background-size: calc(var(--cell) * 3.4);
  background-position:
    calc(var(--x) * var(--cell) * -1)
    calc(var(--y) * var(--cell) * -1);
  box-shadow: inset 0 0 0 1px rgba(120, 86, 48, 0.16);
}

.cell.floor::after {
  content: "";
  position: absolute;
  inset: 0;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.16);
  pointer-events: none;
}

.cell.has-bowl::before {
  content: "";
  position: absolute;
  left: 12%;
  right: 12%;
  top: 18%;
  bottom: 10%;
  border-radius: 40%;
  border: 3px solid var(--kind);
  opacity: 0.85;
  pointer-events: none;
}

.cell.settled::before {
  background: color-mix(in srgb, var(--kind) 28%, transparent);
  box-shadow: 0 0 12px var(--kind);
}

.bowl-sprite {
  position: absolute;
  left: 50%;
  bottom: 6%;
  width: 78%;
  transform: translateX(-50%);
  pointer-events: none;
}

.bowl-sprite.fallback, .crate-sprite.fallback, .player-sprite.fallback {
  display: grid;
  place-items: center;
  color: white;
  font-weight: 800;
  border-radius: 12px;
}

.bowl-sprite.fallback {
  width: 70%;
  height: 46%;
  bottom: 18%;
}

.badge {
  position: absolute;
  right: 4px;
  bottom: 4px;
  min-width: 16px;
  height: 16px;
  padding: 0 4px;
  border-radius: 999px;
  color: white;
  font-size: 10px;
  font-weight: 800;
  display: grid;
  place-items: center;
  z-index: 2;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
}

.actors { position: absolute; inset: 0; pointer-events: none; }

.actor { position: absolute; width: var(--cell); height: var(--cell); }

.crate-sprite {
  position: absolute;
  left: 50%;
  bottom: 2%;
  height: 96%;
  width: auto;
  max-width: 100%;
  transform: translateX(-50%);
  filter: drop-shadow(0 5px 2px rgba(80, 48, 20, 0.28));
}

.crate-sprite.fallback {
  width: 74%;
  height: 74%;
  bottom: 10%;
}

.player-sprite {
  position: absolute;
  left: 50%;
  bottom: 0;
  height: 138%;
  width: auto;
  max-width: none;
  transform: translateX(-50%);
  filter: drop-shadow(0 6px 2px rgba(80, 48, 20, 0.28));
}

.player.flip .player-sprite { transform: translateX(-50%) scaleX(-1); }

.player-sprite.fallback {
  width: 70%;
  height: 90%;
  background: #c47a45;
}

.crate.shake .crate-sprite { animation: shake 0.28s linear; }

.crate.hinted::after {
  content: "";
  position: absolute;
  inset: 2px;
  border: 3px solid #e2a122;
  border-radius: 14px;
  box-shadow: 0 0 0 4px rgba(226, 161, 34, 0.28);
  animation: hintpulse 0.7s ease-in-out infinite;
}

@keyframes hintpulse {
  50% { transform: scale(1.06); }
}

@keyframes shake {
  0%, 100% { transform: translateX(-50%); }
  25% { transform: translateX(calc(-50% - 5px)); }
  75% { transform: translateX(calc(-50% + 5px)); }
}

.controls {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: end;
  gap: 8px;
  padding-top: 6px;
  flex-shrink: 0;
}

.side-actions { display: flex; }

.side-actions.right { justify-content: flex-end; }

.dpad {
  display: grid;
  grid-template-columns: 58px 58px 58px;
  grid-template-areas:
    ". up ."
    "left down right";
  gap: 6px;
  touch-action: none;
}

.pad {
  width: 58px;
  height: 52px;
  border-radius: 16px;
  border: 2px solid var(--line);
  background: var(--paper);
  box-shadow: 0 3px 0 #e4d0b6;
  font-size: 20px;
  font-weight: 800;
  touch-action: none;
}

.pad.up { grid-area: up; }
.pad.down { grid-area: down; }
.pad.left { grid-area: left; }
.pad.right { grid-area: right; }

.keys {
  text-align: center;
  color: var(--ink-soft);
  font-size: 12px;
  margin: 6px 0 0;
  flex-shrink: 0;
}

@media (max-height: 720px) {
  .keys { display: none; }
  .legend-art { width: 28px; height: 28px; }
}

.modal-back {
  position: fixed;
  inset: 0;
  background: rgba(72, 48, 30, 0.38);
  display: grid;
  place-items: center;
  padding: 18px;
  z-index: 30;
}

.modal {
  width: min(420px, 100%);
  background: var(--paper);
  border-radius: 24px;
  padding: 22px 20px 18px;
  text-align: center;
  box-shadow: var(--shadow);
}

.modal h2 {
  margin: 0;
  letter-spacing: 0;
  text-transform: none;
  font-size: 28px;
  color: var(--ink);
}

.modal-stars {
  margin: 8px 0;
  font-size: 36px;
  letter-spacing: 4px;
  color: var(--gold-dim);
}

.modal-stars.s1, .modal-stars.s2 { color: var(--gold); }

.modal p { margin: 0 0 16px; line-height: 1.45; }

.modal-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 124px;
  transform: translateX(-50%) translateY(12px);
  background: #5a4030;
  color: #fffaf3;
  padding: 10px 14px;
  border-radius: 999px;
  font-weight: 700;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
  z-index: 40;
  max-width: min(520px, calc(100% - 24px));
  text-align: center;
}

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

.preload {
  position: fixed;
  left: 14px;
  bottom: 14px;
  z-index: 80;
  margin: 0;
  pointer-events: none;
  background: var(--paper);
  color: var(--ink);
  font-size: 13px;
  font-weight: 800;
  padding: 6px 10px;
  border-radius: 999px;
  box-shadow: var(--shadow);
}

.ad-card img {
  width: 100%;
  height: 140px;
  object-fit: cover;
  border-radius: 16px;
}

.ad-count {
  font-size: 42px;
  font-weight: 800;
  margin: 8px 0 12px;
}

.dance {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 24px 16px 20px;
  background:
    linear-gradient(rgba(243, 230, 212, 0.2), rgba(90, 60, 30, 0.28)),
    url("../assets/sprites/floor.png?v=3.5");
  background-size: auto, 280px;
  text-align: center;
}

.dance-kicker {
  margin: 0;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  font-size: 13px;
  font-weight: 800;
  color: var(--ink-soft);
}

.dance h2 {
  margin: 0;
  letter-spacing: 0;
  text-transform: none;
  font-size: clamp(26px, 4vw, 40px);
  color: var(--ink);
}

.dance-note { margin: 0 0 8px; color: var(--ink-soft); max-width: 520px; }

.dance-status,
.story-status {
  margin: 0;
  font-weight: 800;
  color: var(--ink);
}

.story-art.is-loading { opacity: 0; }

.dance-video {
  width: min(720px, 100%);
  height: min(52vh, 400px);
  object-fit: contain;
  background: #2a2118;
  border-radius: 18px;
}

.dancer {
  width: min(180px, 28vw);
  height: 78%;
  object-fit: contain;
  transform-origin: 50% 100%;
}

.dance-russian .dancer { animation: hopak 0.7s ease-in-out infinite; }
.dance-japanese .dancer { animation: bon 1.6s ease-in-out infinite; }
.dance-disco .dancer { animation: disco 0.5s ease-in-out infinite; }
.dance-trio .dancer { animation: trio 0.48s ease-in-out infinite; }
.dance-trio .dancer:nth-child(2) { animation-delay: 0.12s; }
.dance-trio .dancer:nth-child(3) { animation-delay: 0.24s; }

.disco-ball {
  position: absolute;
  top: 18px;
  width: 54px;
  height: 54px;
  border-radius: 50%;
  background: conic-gradient(#f0b56a, #6f8f72, #d4533e, #2b8b96, #f0b56a);
  animation: spin 1.2s linear infinite;
}

.dance-stage { position: relative; }

@keyframes hopak {
  0%, 100% { transform: translateY(0) rotate(0deg); }
  35% { transform: translateY(-28px) rotate(-12deg); }
  70% { transform: translateY(6px) rotate(14deg); }
}

@keyframes bon {
  0%, 100% { transform: translateX(0) rotate(0deg); }
  50% { transform: translateX(18px) rotate(6deg); }
}

@keyframes disco {
  0%, 100% { transform: translateY(0) rotate(-8deg); }
  50% { transform: translateY(-16px) rotate(10deg); }
}

@keyframes trio {
  0%, 100% { transform: translateY(0) scaleX(1); }
  50% { transform: translateY(-22px) scaleX(1.04); }
}

@keyframes spin { to { transform: rotate(360deg); } }

.dance-actions { display: flex; gap: 10px; margin-top: 12px; }

.hint-play {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 10px;
  margin: 0 auto 6px;
  font-weight: 800;
}

.btn.danger {
  background: #c4503c;
  border-color: #a33b2a;
  color: #fffaf3;
  box-shadow: 0 3px 0 #8d3224;
}

.story {
  position: fixed;
  inset: 0;
  z-index: 70;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(72, 48, 30, 0.48);
}

.story-card {
  width: min(640px, 100%);
  max-height: calc(100dvh - 32px);
  overflow: auto;
  background: var(--paper);
  border-radius: 24px;
  padding: 14px 14px 16px;
  box-shadow: var(--shadow);
  text-align: center;
}

.story-art {
  width: 100%;
  height: min(46vh, 320px);
  object-fit: contain;
  object-position: center;
  background: #e7d3b8;
  border-radius: 16px;
  display: block;
}

.story-kicker {
  margin: 12px 0 0;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  font-size: 13px;
  font-weight: 800;
  color: var(--ink-soft);
}

.story-en {
  margin: 8px 0 0;
  font-size: 18px;
  line-height: 1.45;
  font-weight: 800;
}

.story-dots {
  margin: 10px 0 0;
  font-size: 13px;
  font-weight: 800;
  color: var(--ink-soft);
}

.story-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  margin-top: 14px;
}

@media (max-height: 720px) {
  .story-art { height: min(32vh, 200px); }
  .story-en { font-size: 16px; }
}

:focus-visible {
  outline: 3px solid var(--sage);
  outline-offset: 2px;
}

@media (max-width: 640px) {
  .char-art, .char-art.fallback { height: 120px; }
  .char-blurb { display: none; }
  .cover { height: 168px; }
  .play-head .btn { padding: 8px 10px; }
  .savings-readout { font-size: 16px; }
  .hud .savings-readout { font-size: 15px; }
  .pad, .dpad { width: auto; }
  .dpad { grid-template-columns: 64px 64px 64px; }
  .pad { width: 64px; height: 56px; }
}

@media (prefers-reduced-motion: reduce) {
  .crate.shake .crate-sprite { animation: none; }
  .toast { transition: none; }
}
