:root {
  --ink: #2a2f4f;
  --ink-2: #5a6080;
  --paper: #fbf8ef;
  --paper-2: #efe9d8;
  --bg: #1b1f3a;
  --teal: #2bb3a3;
  --teal-d: #1f8a7e;
  --gold: #f5c542;
  --red: #e8483f;
  --shadow: #0e1124;
  font-family: "Trebuchet MS", "Segoe UI", system-ui, sans-serif;
  color: var(--ink);
}

* { box-sizing: border-box; }
html, body {
  margin: 0; height: 100%;
  background: var(--bg);
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
  user-select: none; -webkit-user-select: none;
}
button { font: inherit; color: inherit; cursor: pointer; }
button:disabled { cursor: default; }
img { image-rendering: pixelated; }

#app {
  display: flex; flex-direction: column;
  height: 100dvh; max-width: 1180px; margin: 0 auto;
  padding: 8px 8px calc(8px + env(safe-area-inset-bottom));
  gap: 8px;
}

/* ---------- HUD ---------- */
#hud { display: flex; gap: 8px; align-items: stretch; }
.stat {
  background: var(--paper); border: 3px solid var(--ink); border-radius: 12px;
  box-shadow: 0 3px 0 var(--shadow);
  display: flex; align-items: center; gap: 8px; padding: 4px 8px 4px 8px;
  min-width: 0;
}
.stat.money { flex: 1.3; }
.stat.orbs { flex: 1; }
.stat b { display: block; font-size: 19px; line-height: 1.05; font-weight: 900; }
.stat small { display: block; font-size: 11px; color: var(--ink-2); font-weight: 700; white-space: nowrap; }
.coin, .orb-ico { flex: none; width: 24px; height: 24px; border-radius: 50%; border: 2px solid var(--ink); }
.coin { background: radial-gradient(circle at 35% 30%, #fff6c8 0 18%, #f5c542 20% 60%, #d89a1c 62%); }
.orb-ico { background: radial-gradient(circle at 35% 30%, #e9fffb 0 15%, #5ee0cf 16% 45%, #1f8a7e 80%); box-shadow: 0 0 0 2px var(--gold) inset; }
.plus {
  margin-left: auto; background: var(--gold); border: 2px solid var(--ink); border-radius: 8px;
  font-weight: 900; font-size: 16px; line-height: 1; padding: 3px 7px; box-shadow: 0 2px 0 var(--ink);
  display: flex; flex-direction: column; align-items: center;
}
.plus small { font-size: 9px; }
.plus:disabled { filter: grayscale(1); opacity: .55; }
.icon-btn {
  flex: none; width: 44px; background: var(--paper); border: 3px solid var(--ink); border-radius: 12px;
  box-shadow: 0 3px 0 var(--shadow); font-size: 20px; font-weight: 900;
}
.icon-btn.off { opacity: .45; text-decoration: line-through; }

/* ---------- scene ---------- */
#sceneWrap {
  position: relative; flex: 1 1 46%; min-height: 190px;
  border: 3px solid var(--ink); border-radius: 14px; overflow: hidden;
  box-shadow: 0 3px 0 var(--shadow); background: #74bf4a;
}
#view { width: 100%; height: 100%; display: block; image-rendering: pixelated; touch-action: manipulation; }
#habTabs {
  position: absolute; left: 6px; right: 6px; bottom: 6px;
  display: flex; gap: 4px; overflow-x: auto; scrollbar-width: none; pointer-events: none;
}
#habTabs::-webkit-scrollbar { display: none; }
.chip {
  pointer-events: auto; flex: none;
  background: rgba(251, 248, 239, .88); border: 2px solid var(--ink); border-radius: 999px;
  font-size: 11px; font-weight: 800; padding: 3px 9px 3px 7px; display: flex; align-items: center; gap: 5px;
}
.chip::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--c); border: 1px solid var(--ink); }
.chip.on { background: var(--ink); color: #fff; }
#floats { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
.float {
  position: absolute; transform: translate(-50%, 0);
  font-weight: 900; font-size: 13px; color: #fff; white-space: nowrap;
  text-shadow: 0 2px 0 var(--ink), 2px 0 0 var(--ink), -2px 0 0 var(--ink), 0 -2px 0 var(--ink);
  animation: floatUp 1.1s ease-out forwards;
}
.float.soft { font-size: 11px; color: #fff6c8; opacity: .9; }
.float.tap { font-size: 16px; color: var(--gold); }
@keyframes floatUp { from { transform: translate(-50%, 0); opacity: 1; } to { transform: translate(-50%, -34px); opacity: 0; } }

#wildBtn {
  position: absolute; transform: translate(-50%, -110%);
  width: 38px; height: 38px; border-radius: 50%;
  background: #fff; border: 3px solid var(--ink); box-shadow: 0 3px 0 var(--ink);
  font-size: 22px; font-weight: 900; color: var(--red);
  animation: bounce .7s ease-in-out infinite;
}
#wildBtn::after {
  content: ""; position: absolute; left: 50%; bottom: -9px; margin-left: -5px;
  border: 5px solid transparent; border-top-color: var(--ink);
}
@keyframes bounce { 50% { transform: translate(-50%, -135%); } }

/* ---------- side panel ---------- */
#side { flex: 1 1 54%; display: flex; flex-direction: column; min-height: 0; }
#tabs { display: flex; gap: 4px; padding: 0 6px; }
#tabs button {
  position: relative; flex: 1; padding: 8px 4px 6px; font-weight: 900; font-size: 14px;
  background: var(--paper-2); border: 3px solid var(--ink); border-bottom: none; border-radius: 12px 12px 0 0;
  color: var(--ink-2);
}
#tabs button.on { background: var(--paper); color: var(--ink); }
#tabs button.ping::after {
  content: ""; position: absolute; top: 5px; right: 6px; width: 9px; height: 9px; border-radius: 50%;
  background: var(--red); border: 2px solid var(--paper); animation: pulse 1s infinite;
}
@keyframes pulse { 50% { transform: scale(1.3); } }
#panel {
  flex: 1; overflow-y: auto; min-height: 0;
  background: var(--paper); border: 3px solid var(--ink); border-radius: 12px;
  box-shadow: 0 3px 0 var(--shadow); padding: 8px;
  scrollbar-width: thin;
}

.seg { display: flex; gap: 4px; margin-bottom: 8px; }
.seg button {
  flex: 1; padding: 5px 0; font-weight: 900; font-size: 13px;
  background: var(--paper-2); border: 2px solid var(--ink); border-radius: 8px;
}
.seg button.on { background: var(--ink); color: #fff; }

.row, .hab {
  display: flex; align-items: center; gap: 8px;
  background: #fff; border: 2px solid var(--ink); border-radius: 10px;
  padding: 6px 8px 6px 4px; margin-bottom: 6px;
}
.row .spr { width: 56px; height: 56px; flex: none; background: var(--paper-2); border-radius: 8px; }
.row.mystery { background: var(--paper-2); border-style: dashed; }
.row.mystery .spr { opacity: .75; }
.info { flex: 1; min-width: 0; }
.nm { font-weight: 900; font-size: 15px; display: flex; align-items: center; gap: 5px; flex-wrap: wrap; }
.sub { font-size: 12px; font-weight: 700; color: var(--ink-2); margin: 1px 0 3px; }
.tiny { font-size: 11px; color: var(--ink-2); font-weight: 600; }
.el {
  font-size: 9px; font-weight: 900; text-transform: uppercase; letter-spacing: .04em;
  color: #fff; background: var(--c); border-radius: 4px; padding: 1px 5px; border: 1px solid rgba(0,0,0,.25);
}
.rar { font-size: 9px; font-weight: 900; text-transform: uppercase; border-radius: 4px; padding: 1px 5px; margin-left: 4px; border: 1px solid var(--ink); }
.r-common { background: #e8e8e8; }
.r-rare { background: #bfe0ff; }
.r-epic { background: #e3c9ff; }
.r-legendary { background: var(--gold); }
.shiny { color: #e0a000; font-weight: 900; }
.bar { height: 7px; background: var(--paper-2); border: 1.5px solid var(--ink); border-radius: 99px; overflow: hidden; }
.bar i { display: block; height: 100%; background: linear-gradient(90deg, var(--teal), #6fe0a0); transition: width .2s; }
.buy {
  flex: none; width: 88px; padding: 6px 4px; border-radius: 10px;
  background: var(--teal); color: #fff; border: 2px solid var(--ink); box-shadow: 0 3px 0 var(--ink);
  display: flex; flex-direction: column; align-items: center; line-height: 1.15;
}
.buy .n { font-size: 12px; font-weight: 800; }
.buy .c { font-size: 14px; font-weight: 900; }
.buy:active:not(:disabled) { transform: translateY(2px); box-shadow: 0 1px 0 var(--ink); }
.buy:disabled { background: #b9bccb; }

.hab .swatch {
  width: 52px; height: 52px; flex: none; border-radius: 8px; border: 2px solid var(--ink);
  background:
    repeating-linear-gradient(90deg, var(--g2) 0 2px, transparent 2px 5px) bottom / 100% 40% no-repeat,
    var(--g);
}
.hab.locked { background: var(--paper-2); }
.hab.locked .swatch { filter: grayscale(.8) brightness(.8); }

.btn {
  padding: 8px 12px; font-weight: 900; font-size: 14px; border-radius: 10px;
  background: var(--paper); border: 2px solid var(--ink); box-shadow: 0 3px 0 var(--ink);
  white-space: nowrap;
}
.btn.primary { background: var(--teal); color: #fff; }
.btn.big { display: block; width: 100%; padding: 12px; font-size: 17px; margin-top: 10px; }
.btn:disabled { background: #d5d7e0; color: var(--ink-2); box-shadow: 0 3px 0 #9a9db0; border-color: #9a9db0; }
.btn:active:not(:disabled) { transform: translateY(2px); box-shadow: 0 1px 0 var(--ink); }

.dexhead { font-size: 13px; font-weight: 700; margin: 2px 2px 8px; }
.dex { display: grid; grid-template-columns: repeat(auto-fill, minmax(76px, 1fr)); gap: 6px; }
.dx {
  background: #fff; border: 2px solid var(--ink); border-radius: 8px; padding: 4px 2px 5px;
  display: flex; flex-direction: column; align-items: center;
}
.dx img { width: 56px; height: 56px; }
.dx .no { font-size: 9px; font-weight: 800; color: var(--ink-2); }
.dx .dn { font-size: 11px; font-weight: 900; text-align: center; }
.dx.unk { background: var(--paper-2); }
.dx.unk img { opacity: .6; }

.journey { padding: 4px 4px 8px; }
.journey p { margin: 8px 0; }
.badges { display: flex; flex-wrap: wrap; gap: 4px; min-height: 26px; }
.badge {
  width: 24px; height: 24px; display: block;
  background: var(--gold); border: 2px solid var(--ink);
  clip-path: polygon(50% 0, 61% 35%, 98% 35%, 68% 57%, 79% 91%, 50% 70%, 21% 91%, 32% 57%, 2% 35%, 39% 35%);
}
.jstats { display: flex; gap: 8px; }
.jstats div { flex: 1; background: #fff; border: 2px solid var(--ink); border-radius: 10px; padding: 6px 8px; }
.jstats b { display: block; font-size: 18px; }

/* ---------- overlays ---------- */
#overlay {
  position: fixed; inset: 0; z-index: 10;
  display: flex; align-items: center; justify-content: center;
  background: rgba(14, 17, 36, .55); padding: 12px;
  animation: fadeIn .2s;
}
#overlay[hidden] { display: none; }
#overlay.evo-bg { background: rgba(14, 17, 36, .9); }
@keyframes fadeIn { from { opacity: 0; } }
.box {
  width: min(94vw, 460px); max-height: 94dvh; overflow-y: auto;
  background: var(--paper); border: 4px solid var(--ink); border-radius: 16px;
  box-shadow: 0 5px 0 var(--shadow); padding: 16px; text-align: center;
  animation: popIn .25s ease-out;
}
@keyframes popIn { from { transform: scale(.85); opacity: 0; } }
.box h2 { margin: 0 0 4px; font-size: 22px; font-weight: 900; }
.box p { margin: 6px 0; font-weight: 600; }
.acts { display: flex; gap: 8px; justify-content: center; margin-top: 12px; }
.acts .btn { flex: 1; }
.earned { font-size: 28px; font-weight: 900; display: flex; align-items: center; justify-content: center; gap: 8px; margin: 10px 0; }
.earned .coin { width: 30px; height: 30px; }

.starters { display: flex; gap: 8px; margin: 12px 0; }
.scard {
  flex: 1; background: #fff; border: 3px solid var(--ink); border-radius: 12px; padding: 8px 4px 10px;
  box-shadow: 0 4px 0 var(--c); display: flex; flex-direction: column; align-items: center; gap: 4px;
  transition: transform .12s;
}
.scard:hover { transform: translateY(-3px); }
.scard img { width: 96px; height: 96px; animation: idle 1.4s ease-in-out infinite; }
.scard:nth-child(2) img { animation-delay: .2s; }
.scard:nth-child(3) img { animation-delay: .4s; }
@keyframes idle { 50% { transform: translateY(-4px); } }

.evo { background: #1b1f3a; color: #fff; border-color: #fff; }
.evo .tiny { color: #c9cde0; }
.evo-stage { height: 180px; display: flex; align-items: center; justify-content: center; }
.evo-stage img { width: 170px; height: 170px; }
.evo-stage img.white { filter: brightness(0) invert(1) drop-shadow(0 0 12px #fff); }
.evo-stage img.pop { animation: evoPop .5s ease-out; }
@keyframes evoPop { 0% { transform: scale(1.3); filter: brightness(3); } }
.evo .msg { font-size: 17px; min-height: 48px; }

/* battle */
.battle {
  width: min(94vw, 480px); background: var(--paper); border: 4px solid var(--ink); border-radius: 16px;
  overflow: hidden; box-shadow: 0 5px 0 var(--shadow); animation: popIn .25s ease-out;
}
.arena {
  position: relative; height: 270px; overflow: hidden;
  background: linear-gradient(#bfe6ff, #eaf7ff 58%, var(--g) 58%);
}
.plat {
  position: absolute; left: 50%; top: 186px; width: 210px; height: 46px; margin-left: -105px;
  border-radius: 50%; background: var(--p); box-shadow: inset 0 -7px 0 rgba(0,0,0,.14), 0 0 0 3px rgba(42,47,79,.25);
}
.wild-spr { position: absolute; left: 50%; top: 50px; width: 160px; height: 160px; margin-left: -80px; }
.wild-spr.in { animation: slideIn .6s ease-out; }
.wild-spr.absorb { animation: absorb .38s forwards; }
.wild-spr.appear { animation: appear .35s; }
.wild-spr.flee { animation: flee .5s forwards; }
@keyframes slideIn { from { transform: translateX(300px); } }
@keyframes absorb { to { filter: brightness(0) invert(1); transform: scale(0) translateY(90px); opacity: 0; } }
@keyframes appear { from { filter: brightness(0) invert(1); transform: scale(.2) translateY(90px); } }
@keyframes flee { to { transform: translateX(320px); opacity: 0; } }
.ring { position: absolute; left: 50%; top: 88px; width: 160px; height: 160px; margin-left: -80px; pointer-events: none; }
.nameplate {
  position: absolute; left: 10px; top: 10px; z-index: 2;
  background: var(--paper); border: 3px solid var(--ink); border-radius: 12px 4px 12px 4px;
  padding: 4px 10px; font-weight: 800; box-shadow: 0 3px 0 rgba(0,0,0,.2);
}
.orb {
  position: absolute; left: calc(50% - 14px); top: 290px; width: 28px; height: 28px; border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #e9fffb 0 15%, #5ee0cf 16% 45%, #1f8a7e 80%);
  border: 3px solid var(--gold); box-shadow: 0 0 0 2px var(--ink); opacity: 0; z-index: 3;
}
.orb.fly { opacity: 1; animation: fly .52s ease-out forwards; }
@keyframes fly {
  0% { top: 290px; transform: scale(1.5) rotate(0); }
  55% { top: 40px; transform: scale(1.1) rotate(400deg); }
  100% { top: 152px; transform: scale(1) rotate(720deg); }
}
.orb.sit { opacity: 1; top: 152px; transition: top .35s cubic-bezier(.5, 0, .75, 0); }
.orb.sit.open { box-shadow: 0 0 0 2px var(--ink), 0 0 24px 10px rgba(255,255,255,.9); }
.orb.drop { top: 182px; }
.orb.shake { animation: shake .5s ease-in-out; }
@keyframes shake { 20% { transform: rotate(-28deg); } 60% { transform: rotate(24deg); } 100% { transform: rotate(0); } }
.orb.caught { filter: brightness(.85); box-shadow: 0 0 0 2px var(--ink), 0 0 0 7px rgba(245,197,66,.6); animation: caught .6s; }
@keyframes caught { 30% { transform: scale(1.25); } }
.orb.burst { opacity: 1; top: 182px; animation: burst .35s forwards; }
@keyframes burst { to { transform: scale(2.4); opacity: 0; } }
.qual {
  position: absolute; left: 50%; top: 14px; transform: translateX(-50%); z-index: 4;
  font-size: 24px; font-weight: 900; color: #fff; opacity: 0;
  text-shadow: 0 3px 0 var(--ink), 3px 0 0 var(--ink), -3px 0 0 var(--ink), 0 -3px 0 var(--ink);
}
.qual.show { animation: qual .9s ease-out forwards; }
@keyframes qual { 0% { opacity: 0; transform: translate(-50%, 10px) scale(.6); } 25% { opacity: 1; transform: translate(-50%, 0) scale(1.1); } 80% { opacity: 1; } 100% { opacity: 0; } }
.textbox { border-top: 4px solid var(--ink); padding: 12px 14px 14px; background: #fff; }
.textbox .msg { font-weight: 800; font-size: 17px; min-height: 46px; margin: 0; }
.textbox .acts { margin-top: 10px; }
.acts.hide { visibility: hidden; }

#toasts { position: fixed; left: 0; right: 0; top: 70px; z-index: 20; display: flex; flex-direction: column; align-items: center; gap: 6px; pointer-events: none; }
.toast {
  background: var(--ink); color: #fff; font-weight: 700; font-size: 14px; padding: 8px 14px;
  border-radius: 10px; border: 2px solid #fff; animation: popIn .2s; transition: opacity .4s, transform .4s;
  max-width: 92vw; text-align: center;
}
.toast.out { opacity: 0; transform: translateY(-10px); }

/* landscape: scene left, panel right */
@media (min-aspect-ratio: 5/4) and (min-width: 700px) {
  #app { display: grid; grid-template-columns: 1.25fr 1fr; grid-template-rows: auto 1fr; }
  #hud { grid-column: 1 / -1; }
  #sceneWrap { grid-column: 1; grid-row: 2; min-height: 0; }
  #side { grid-column: 2; grid-row: 2; }
}
@media (max-width: 380px) {
  .stat b { font-size: 16px; }
  .buy { width: 76px; }
  .row .spr { width: 48px; height: 48px; }
}
